You can achieve this by following code,
$integerIDs = array_map('intval', explode(',', $string));
Answer from Mark Baker on Stack OverflowConverting an array of string to array of int in PHP - Stack Overflow
convert string array to integer array in php - Stack Overflow
Converting array of string to array of integers in PHP - Stack Overflow
String to array of Integers php - Stack Overflow
You can achieve this by following code,
$integerIDs = array_map('intval', explode(',', $string));
So I was curious about the performance of some of the methods mentioned in the answers for large number of integers.
Preparation
Just creating an array of 1 million random integers between 0 and 100. Than, I imploded them to get the string.
$integers = array();
for (
i < 1000000; $i++) {
$integers[] = rand(0, 100);
}
$long_string = implode(',', $integers);
Method 1
This is the one liner from Mark's answer:
$integerIDs = array_map('intval', explode(',', $long_string));
Method 2
This is the JSON approach:
$integerIDs = json_decode('[' . $long_string . ']', true);
Method 3
I came up with this one as modification of Mark's answer. This is still using explode() function, but instead of calling array_map() I'm using regular foreach loop to do the work to avoid the overhead array_map() might have. I am also parsing with (int) vs intval(), but I tried both, and there is not much difference in terms of performance.
$result_array = array();
$strings_array = explode(',', $long_string);
foreach ($strings_array as $each_number) {
$result_array[] = (int) $each_number;
}
Results
Method 1 Method 2 Method 3
0.4804770947 0.3608930111 0.3387751579
0.4748001099 0.363986969 0.3762528896
0.4625790119 0.3645150661 0.3335959911
0.5065748692 0.3570590019 0.3365750313
0.4803431034 0.4135499001 0.3330330849
0.4510772228 0.4421861172 0.341176033
0.503674984 0.3612480164 0.3561749458
0.5598649979 0.352314949 0.3766179085
0.4573421478 0.3527538776 0.3473439217
0.4863037268 0.3742785454 0.3488383293
The bottom line is the average. It looks like the first method was a little slower for 1 million integers, but I didn't notice 3x performance gain of Method 2 as stated in the answer. It turned out foreach loop was the quickest one in my case. I've done the benchmarking with Xdebug.
Edit: It's been a while since the answer was originally posted. To clarify, the benchmark was done in php 5.6.40.
Use array_map i.e.
$array = array_map(function($n) { return (int)$i; }, $array);
So Ed-Heal already sent a really good answer, and more efficient, but I will post one with a for loop just to explain the process:
$stringArray = array("2","5"); // We set up the string array
var_dump($stringArray);
// We loop through the array:
for ($i = 0; $i<sizeof($stringArray); $i++) {
// We set the value at the index $i to an integer.
$stringArray[$i] = intval($stringArray[$i]);
}
var_dump($stringArray);
The output of the code above would be as follows
array(2) { [0]=> string(1) "2" [1]=> string(1) "5" }
array(2) { [0]=> int(2) [1]=> int(5) }
So what does this code do? Well we loop through the array, we grab every value and change it with a integer with the function intval().
Oh and please, don't capsize your question's title.
Simplified version of @george's answer would be:
$TaxIds = array_map('intval', $TaxIds);
More info at array_map
Although the question was asked a long time ago, it may be useful for someone
You can use array_map
$TaxIds = array_map(function($value) {
return intval($value);
}, $TaxIds);
You should use array_map instead of array_walk:
$numArray = array_map('intval', $numArray);
If you still want to use array_walk - refer to a manual which says:
If callback needs to be working with the actual values of the array, specify the first parameter of callback as a reference. Then, any changes made to those elements will be made in the original array itself.
As intval function doesn't work with references you need to wrap it in some other logics, something like:
array_walk($numArray, function(&$v){ $v = intval($v); });
// which is the same as @BizzyBob solution)))
intval() does not set the value, it only returns the value.
You could do this:
array_walk($numArray, function(&$x){$x = intval($x);});
Use PHP's explode.
arr = explode("," $str); // array( '1', '2', '3', '4', '5', '6' );
foreach (
index => $value)
index] = (int)$value;
// casts each value to integer type -- array( 1, 2, 3, 4, 5, 6 );
As suggested by Tim Cooper, using array_walk is simpler than the above loop:
array_walk($arr, 'intval');
return array_map('intval', explode(",", '1,2,3,4,5,6,7,8,9'));
Example: https://3v4l.org/RmAXf
It is clearly documented - I guess I just never paid close enough attention before: https://www.php.net/manual/en/language.types.array.php#language.types.array.syntax
But I am wondering if anybody knows why? Was this an intentional decision?
You can use str_split and intval:
$number = 2468;
$array = array_map('intval', str_split($number));
var_dump($array);
Which will give the following output:
array(4) {
[0] => int(2)
[1] => int(4)
[2] => int(6)
[3] => int(8)
}
Demo
use str_split() function
$array = str_split($str);
http://php.net/manual/en/function.str-split.php