Skip to content

Instantly share code, notes, and snippets.

@bmakowski
Created June 27, 2018 09:39
Show Gist options
  • Save bmakowski/6b25f5a8731870202adcffb1cb587317 to your computer and use it in GitHub Desktop.
Save bmakowski/6b25f5a8731870202adcffb1cb587317 to your computer and use it in GitHub Desktop.
Convert an array of strings into an array of integers
<?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(&amp;$array,
create_function('&amp;$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