array_map to intval ;-)
$array = ["123", "34", "223"];
$integerIDs = array_map('intval', $array);
Hello ,
How to convert this string
'["382","266","18","60"]'
to int array
[382,266,18,60]
Thanks
array_map to intval ;-)
$array = ["123", "34", "223"];
$integerIDs = array_map('intval', $array);
thanks ,
but ' ["123", "34", "223"]' is not array by default , it's a string !
Continue the code from Brauson only use json_decode first in your string.
$integerIDs = array_map('intval', json_decode($string, true));
just json_decode should do it?
@snapey is right, json_decode('["382","266","18","60"]') seems like the cleanest way to do it. No need for a mapping or the associative argument.
It necessary when he really wants an array of integers, if he don´t use it it will only be an array of strings even that the value seems to be an integer.
Good call @wafto . I just double checked and you're 100% right, see below:
array_map('intval', json_decode('["382","266","18","60"]'));
Output:
array:4 [▼
0 => 382
1 => 266
2 => 18
3 => 60
]
Whereas:
json_decode('["382","266","18","60"]')
Output (strings):
array:4 [▼
0 => "382"
1 => "266"
2 => "18"
3 => "60"
]
Thanks @aswinghosh , you should mark @wafto below answer as correct, since he got it first and most accurately.
https://laracasts.com/discuss/channels/laravel/convert-string-array-to-array-of-ints?reply=525914
Please or to participate in this conversation.