Created
June 27, 2018 09:39
-
-
Save bmakowski/6b25f5a8731870202adcffb1cb587317 to your computer and use it in GitHub Desktop.
Convert an array of strings into an array of integers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Traditional way | |
$array = array('0', '1', '2'); | |
foreach ($array as $key => $var) { | |
$array[$key] = (int)$var; | |
} | |
// The nice way | |
$array = array('0', '1', '2'); | |
array_walk(&$array, | |
create_function('&$value', '$value = (int)$value;'); | |
); | |
// Another nice version with array_map() | |
$array = array('0', '1', '2'); | |
$array = array_map( | |
create_function('$value', 'return (int)$value;'), | |
$array | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment