0

I have a string like this: '3,6,43,8'.

I can convert it to an array easily:

$newArray = explode( ',', $string );

but as I need integer elements in the array, I should convert every array element to an integer:

foreach( $newArray as $key => $sNumber ) {
    $newArray[ $key ] = intval( $sNumber );
}

Is there a way to directly convert the array elements to integers, instead of strings?

Manolo
  • 24,020
  • 20
  • 85
  • 130

3 Answers3

5

You can use $inty = array_map('intval', explode(',', $string));

Ali Gajani
  • 14,762
  • 12
  • 59
  • 100
1

Try this one:

<?php

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

?>

Thanks!

Anand Solanki
  • 3,419
  • 4
  • 16
  • 27
0

try this

$val = '3,6,43,8';
$integerIDs = array_map('intval', explode(',', $val));
var_dump($integerIDs);

with var_dump you can check it type.

user2727841
  • 715
  • 6
  • 21