how to convert array values from string to int?

cwal picture cwal · Mar 7, 2012 · Viewed 242.5k times · Source
$string = "1,2,3"
$ids = explode(',', $string);
var_dump($ids);

returns

array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
}

I need for the values to be of type int instead of type string. Is there a better way of doing this than looping through the array with a foreach and converting each string to int?

Answer

Mark Baker picture Mark Baker · Mar 7, 2012

You can achieve this by following code,

$integerIDs = array_map('intval', explode(',', $string));