0

Suppose after applying implode to an array , I got a String for example :-

$var = 1631075,1631076;

On applying var_dump to $var , I received the output as string(15) "1631075,1631076"

how will I convert entire $var into Integer variable . Such that the var_dump display int(15)1631075,1631076

Ajanta
  • 11
  • 1
  • 4

1 Answers1

1
$var = '1631075,1631076';
foreach(explode(',',$var) as $val){
 echo intval($val);
}

or something like

$var = '1631075,1631076';
$integers = explode(',',$var);
$converted = array_map('intval', $integers);
Danny
  • 1,185
  • 2
  • 12
  • 33
  • But Sir ,it is giving the output as 16310751631076; how can I get 1631075,1631076 as integer – Ajanta Jun 08 '18 at 17:37
  • "1631075,1631076" is not an integer and can't be. That can only be a string if you want that inside one variable. – Danny Jun 08 '18 at 17:39
  • Sir actually I want the final output as [1631075,1631076] (integer format) the required array is Array ( [0] => 1631075 [1] => 1631076 ) – Ajanta Jun 08 '18 at 17:45
  • Use "intval()" on the array values before handling mathematical functions. Also look at https://stackoverflow.com/questions/8963910/string-to-array-of-integers-php – Danny Jun 08 '18 at 17:51