16

Say I have a string like so $thestring = "1,2,3,8,2".

If I explode(',', $thestring) it, I get an array of strings. How do I explode it to an array of integers instead?

4

6 Answers 6

44

array_map also could be used:

$s = "1,2,3,8,2";
$ints = array_map('intval', explode(',', $s ));
var_dump( $ints );

Output:

array(5) {
  [0]=>      int(1)
  [1]=>      int(2)
  [2]=>      int(3)
  [3]=>      int(8)
  [4]=>      int(2)
}

Example codepad.

12

Use something like this:

$data = explode( ',', $thestring );
array_walk( $data, 'intval' );

http://php.net/manual/en/function.array-walk.php

4
  • Yes, I noticed I was wrong and was going to edit it back. But I find the PHP docs confusing, it says callable for first arg. Feb 28, 2013 at 12:22
  • 2
    @AmigableClarkKant, see the link, callable is second, first is array. But anyway, yes, this is one of the biggest PHP problems. Some functions are with underscore, some are without. Some have one order, others have reverse. Strange. Feb 28, 2013 at 12:24
  • @Prof.Falken well, i think your thinking of array_map? Jan 13, 2014 at 6:15
  • 6
    This is no longer working in php 5.3 (see comments at php.net/manual/en/function.array-walk.php) . Instead use $data = array_map('intval',$data);
    – ParoX
    Oct 4, 2015 at 21:38
1

For the most part you shouldn't really need to (PHP is generally good with handling casting strings and floats/ints), but if it is absolutely necessary, you can array_walk with intval or floatval:

$arr = explode(',','1,2,3'); 
// use floatval if you think you are going to have decimals
array_walk($arr,'intval'); 
print_r($arr);

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

If you need something a bit more verbose, you can also look into settype:

$arr = explode(",","1,2,3");
function fn(&$a){settype($a,"int");}
array_walk($f,"fn");
print_r($f);

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

That could be particularly useful if you're trying to cast dynamically:

class Converter {
    public $type = 'int';
    public function cast(&$val){ settype($val, $this->type); }
}
$c = new Converter();

$arr = explode(",","1,2,3,0");
array_walk($arr,array($c, 'cast'));
print_r($arr);

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 0
)

// now using a bool
$c->type = 'bool';
$arr = explode(",","1,2,3,0");
array_walk($arr,array($c, 'cast'));
var_dump($arr); // using var_dump because the output is clearer.

array(4) {
  [0]=>
  bool(true)
  [1]=>
  bool(true)
  [2]=>
  bool(true)
  [3]=>
  bool(false)
}
0

Since $thestring is an string then you will get an array of strings.

Just add (int) in front of the exploded values.

Or use the array_walk function:

$arr = explode(',', $thestring);
array_walk($arr, 'intval');
1
0
$thestring = "1,2,3,8,a,b,2";

$newArray = array();
$theArray = explode(",", $thestring);

print_r($theArray);

foreach ($theArray as $theData) {
  if (is_numeric($theData)) {
    $newArray[] = $theData;
  }
}

print_r($newArray);

// Output

Original array

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 8 [4] => a [5] => b [6] => 2 )

Numeric only array

 Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 8 [4] => 2 ) 
0
$arr=explode(',', $thestring);
$newstr   = '';
foreach($arr as $key=>$val){
  $newstr .= $val; 
}
1
  • 3
    I don't think it procudes the expected result, it will produce a string like 12382.
    – biziclop
    Feb 28, 2013 at 12:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.