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?

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173

6 Answers6

42

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.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
biziclop
  • 14,466
  • 3
  • 49
  • 65
12

Use something like this:

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

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

Serge Kuharev
  • 1,052
  • 6
  • 16
  • 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. – Prof. Falken Feb 28 '13 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. – Serge Kuharev Feb 28 '13 at 12:24
  • @Prof.Falken well, i think your thinking of array_map? – Mohammed Joraid Jan 13 '14 at 06:15
  • 6
    This is no longer working in php 5.3 (see comments at http://php.net/manual/en/function.array-walk.php) . Instead use `$data = array_map('intval',$data);` – ParoX Oct 04 '15 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)
}
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
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');
Jonast92
  • 4,964
  • 1
  • 18
  • 32
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 ) 
Dino Babu
  • 5,814
  • 3
  • 24
  • 33
0
$arr=explode(',', $thestring);
$newstr   = '';
foreach($arr as $key=>$val){
  $newstr .= $val; 
}
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91