Converting arrray values to integer from request in laravel

空扰寡人 提交于 2020-12-31 05:53:21

问题


I want to array_diff() two arrays in Laravel. The first array looks like this:

    array:4 [
  0 => 7248
  1 => 7249
  2 => 7250
  3 => 7251
]

the second one:

array:4 [
  0 => "7248"
  1 => "7249"
  2 => "7250"
  3 => "7251"
]

this one I get with $request->request->get('ids', []);.

How can I convert one array to either strings or integers? As these arrays can grow large, I don't really want to convert every single value one at a time.

Update:

array_diff() is doing it's job, altough there are strings vs. integers.

Thanks in advance!


回答1:


$newArray = array_map('intval', $request->request->get('ids', []));

This code will convert your string fields to int so you can compare.



来源:https://stackoverflow.com/questions/54254409/converting-arrray-values-to-integer-from-request-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!