Jocky Doe Jocky Doe - 29 days ago 8
PHP Question

Easy way to parse array values

Is there util that parses array content without the need of iterating it and parsing each value?

input:

['2','3','7']


output:
[2, 3, 7]

Answer

This obviously iterates internally, but you don't have to code an iterator:

$output = array_map('intval', $input);

Maps every value in $input to the intval() function and returns the result. For things that cannot be converted into an integer you'll get 0 or for objects, a notice and that value will not be returned.

I can't tell if you want to remove 0 values or not from your comment, but if so:

$output = array_filter(array_map('intval', $input));
Comments