You can achieve this by following code,

$integerIDs = array_map('intval', explode(',', $string));
Answer from Mark Baker on Stack Overflow
🌐
Laracasts
laracasts.com › discuss › channels › laravel › convert-string-array-to-array-of-ints
convert string array to array of int(s)
How to convert this string · ... Level 18 · ReplyReport Spam · array_map to intval ;-) $array = ["123", "34", "223"]; $integerIDs = array_map('intval', $array); Like Reply ·...
Discussions

Converting an array of string to array of int in PHP - Stack Overflow
Im guessing its because your variables are strings, and need to be integers. You might want to look into variable types, its a simple thing to solve really. Also, no need to shout. ... Save this answer. ... Show activity on this post. Use array_map i.e. More on stackoverflow.com
🌐 stackoverflow.com
convert string array to integer array in php - Stack Overflow
intval($value) : $value }, $arr); ... The OP hasn't indicated that the array will contain strings that can't be parsed as integers, or what needs to be done with such values. If this is something the OP needs, it can be accomplished very easily by adjusting the function being passed to array_map. More on stackoverflow.com
🌐 stackoverflow.com
May 12, 2020
Converting array of string to array of integers in PHP - Stack Overflow
I followed the leads in the questions this and this. I am trying to convert an input stream of numbers to an array of integers. The code should be self explanatory. $handle = fopen("php://stdin... More on stackoverflow.com
🌐 stackoverflow.com
July 1, 2017
String to array of Integers php - Stack Overflow
I wan to convert a string for example 1,2,3,4,5,6 to an array of integers in php? I find functions that only have access to the first character of the string for example 1. More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 16
747

You can achieve this by following code,

$integerIDs = array_map('intval', explode(',', $string));
2 of 16
62

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.

🌐
PHP
php.net › manual › en › function.intval.php
PHP: intval - Manual
After realizing I couldn't just ... The solution for this, and the way I recommend converting a string to an integer, is: $num = $num + 0; and PHP will leave your number alone; it'll just know it's a number....
Find elsewhere
🌐
ReqBin
reqbin.com › code › php › m2rrca1r › php-string-to-int-example
How do I convert a string to int in PHP?
The easiest way to convert strings to numbers in PHP is to use the (int/float/double)$variable cast, which directly converts the string to a number. You can also convert a string to a number using the intval($value, $base), floatval($value), ...
🌐
Sololearn
sololearn.com › en › Discuss › 2094671 › convert-php-array-to-integer
Convert PHP Array to Integer? | Sololearn: Learn to code for FREE!
December 9, 2019 - Answer: Use foreach. foreach is better for iterating through values of an array even if keys from 0, 1, 2, 3, 4, 5, ... are set. This is why foreach is more frequently used than for in PHP. foreach ($max_id as $val) { echo $val .
🌐
Coddy.Tech
coddy.tech › docs › php › type casting
PHP Type Casting: String to Int, intval() and (int) | Coddy
1 month ago - Use implode(", ", $array) or json_encode($array) to turn an array into text. Pick a value to see what intval, (bool), strval, is_numeric and gettype return for it. Every cell was computed by PHP 8.3. intval, (bool), strval and is_numeric for ...
🌐
Simplilearn
simplilearn.com › home › resources › software development › converting string to array in php using different methods
Converting String to Array in PHP Using Different Methods
September 17, 2025 - Understand the need to convert string to array in PHP and explore the complete list of methods used to convert a string to an array in PHP. Start learning now!
Address: 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
PHP Freaks
forums.phpfreaks.com › php coding › php coding help
Converting array to Integer values. - PHP Coding Help - PHP Freaks
September 24, 2010 - Hi there, I am working on an associative array: The array is: $currentIds //when we do print_r($currentIds); //it returns value 41 and 42. Array ( [0] => [1] => 41 [2] => 42 [3] => ) Which is OK but I have to use these two values in an SQL query but they are in Array forms. I need to ...
🌐
W3Schools
w3schools.com › php › func_var_intval.asp
PHP intval() Function
addcslashes() addslashes() bin2hex() chop() chr() chunk_split() convert_cyr_string() convert_uudecode() convert_uuencode() count_chars() crc32() crypt() echo() explode() fprint() get_html_translation_table() hebrev() hebrevc() hex2bin() html_entity_decode() htmlentities() htmlspecialchars_decode() htmlspecialchars() implode() join() lcfirst() levenshtein() localeconv() ltrim() md5() md5_file() metaphone() money_format() nl_langinfo() nl2br() number_format() ord() parse_str() print() printf() quoted_printable_decode() quoted_printable_encode() quotemeta() rtrim() setlocale() sha1() sha1_file()
🌐
TutorialKart
tutorialkart.com › php › php-convert-string-to-int
PHP - Convert string to integer
May 7, 2023 - To convert string to int in PHP, you can use Type Casting method or PHP built-in function intval().
🌐
Coderwall
coderwall.com › p › l7guxq › how-to-convert-string-to-integer-php-mysql
How to convert string to integer (PHP & MySQL) (Example)
December 11, 2021 - $myVar = "13"; var_dump($myVar); // string '13' (length=2) $myVar= intval($myVar); var_dump($myVar); // int 13 $myVar = "13"; var_dump($myVar); // string '13' (length=2) $myVar= (int)$myVar; var_dump($myVar); // int 13 ... SELECT * FROM stop_times LEFT JOIN stops ON stop_times.stop_id=stops.stop_id WHERE stop_times.trip_id=2755013 ORDER BY CAST(stop_times.stop_sequence AS UNSIGNED INTEGER) ASC ... Please don't take advantage of type weirdness in PHP to do this (i.e., $myVar + 0).
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-convert-a-string-into-number-in-php
How to convert a String into Number in PHP ? - GeeksforGeeks
July 11, 2025 - The string number can also be converted into an integer or float by adding 0 to the string. In PHP, performing mathematical operations, the string is converted to an integer or float implicitly.