Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explode comma separated integers to intvals? [duplicate]

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?

like image 711
Prof. Falken Avatar asked Feb 28 '13 12:02

Prof. Falken


Video Answer


1 Answers

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.

like image 78
biziclop Avatar answered Nov 16 '22 02:11

biziclop