String to array of Integers php

25,825

Solution 1

Use PHP's explode.

$str = "1,2,3,4,5,6";
$arr = explode("," $str); // array( '1', '2', '3', '4', '5', '6' );

foreach ($arr AS $index => $value)
    $arr[$index] = (int)$value; 

// casts each value to integer type -- array( 1, 2, 3, 4, 5, 6 );

As suggested by Tim Cooper, using array_walk is simpler than the above loop:

array_walk($arr, 'intval');

Solution 2

return array_map('intval', explode(",", '1,2,3,4,5,6,7,8,9'));

Solution 3

The above answers could potentially return non-numeric values

array_walk & array_map with intval

Both return arrays that are tainted with non-numeric values.

$string = ',g,6,4,3,f,32,a,';
$array = explode(',', $string);
array_walk($array, 'intval');
$arrayMap = array_map('intval', $array);
var_dump($array);
var_dump($arrayMap);
/*
     array(9) {
      [0]=>
      string(0) ""
      [1]=>
      string(1) "g"
      [2]=>
      string(1) "6"
      [3]=>
      string(1) "4"
      [4]=>
      string(1) "3"
      [5]=>
      string(1) "f"
      [6]=>
      string(2) "32"
      [7]=>
      string(1) "a"
      [8]=>
      string(0) ""
    }
 */

array_filter to only return numeric values

$string = ',g,6,4,3,f,32,a,';
$array = explode(',', $string);
$numericOnlyArray = array_filter($array,'is_numeric');

var_dump($numericOnlyArray);

/*
    result:
    array(4) {
      [2]=>
      string(1) "6"
      [3]=>
      string(1) "4"
      [4]=>
      string(1) "3"
      [6]=>
      string(2) "32"
    }
*/

To get only integers

$string = ',g,6,4,3,f,32,a,';
$array = explode(',', $string);
$result = array_map('intval', array_filter($array, 'is_numeric'));

var_dump($result);

/*
    result:
    array(4) {
      [2]=>
      int(6)
      [3]=>
      int(4)
      [4]=>
      int(3)
      [6]=>
      int(32)
    }
    }
*/

Solution 4

explode(",",'1,2,3,4,5,6,7,8,9');

http://www.php.net/manual/en/function.explode.php

Share:
25,825
snake plissken
Author by

snake plissken

Updated on July 22, 2022

Comments

  • snake plissken
    snake plissken almost 2 years

    I wan to convert a string for example 1,2,3,4,5,6 to an array of integers in php? I find functions that only have access to the first character of the string for example 1. How can I conert the whole string to array?

    function read_id_txt()
    {
    
            $handle_file = fopen("temporalfile.txt", 'r'); 
    
            $i=0;
    
            while ($array_var[$i] = fgets($handle_file, 4096)) { 
                echo "<br>";
                print_r($array_var[i]);
                $i++;
            }
    
            fclose($handle_file);   
    
            $temp=explode(" ", $array_var[0]);      
    
            return $temp;   
    
    
    }
    
  • snake plissken
    snake plissken over 12 years
    OK what i ve done is the above $handle_file = fopen("temporalfile.txt", 'r'); $i=0; while ($array_var[$i] = fgets($handle_file, 4096)) { $i++;} fclose($handle_file); $temp=explode(" ", $array_var[0]); print_r($arr); it returns Array ( [0] => 1,2,4,56,6,1,23,34,54,75,43 ) i want to have every value in different cell of the matrix.
  • Tim Cooper
    Tim Cooper over 12 years
    @Josh: That's an array of strings, not integers.
  • Tim Cooper
    Tim Cooper over 12 years
    @Josh: You can simplify that loop with a simple call to array_walk: $arr = array_walk('intval', $arr);.
  • Josh
    Josh over 12 years
    @Tim Good call! I didn't think of intval (doh!), and thought the loop would be simpler than array_walk because of the necessary function definition.
  • snake plissken
    snake plissken over 12 years
    Xm my prob was in exmplode i put the space character instead of ','. With , it works. Thank tou for your time guys.
  • TimWolla
    TimWolla over 12 years
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
  • Riesling
    Riesling over 11 years
    The array_walk thing didn't work for me with php 5.4, but array_map did: $arr = array_map('intval', $arr);
  • Josh
    Josh over 11 years
    @Riesling Keep in mind, array_map will return a an array, whereas array_walk returns a boolean and modifies the original array. Both should work.
  • user2001487
    user2001487 almost 11 years
    array_walk returns a bool. This was the only way I could get my string to convert to an array of int to use in Jpgraph.
  • Roland Soós
    Roland Soós almost 11 years
    array_walk makes the modification on the first parameter, so in @josh solution, the $arr variable will contain the integer array not the return value.
  • CommandZ
    CommandZ about 9 years
    This will return non-numeric values as well because of the use of intval. "" == 0 using intval
  • Ondrej Machulda
    Ondrej Machulda about 8 years
    Beware! This wont work i PHP 5.3+! use $arr = array_map('intval', $arr); instead.
  • mickmackusa
    mickmackusa about 2 years
    No @Josh, array_walk() does NOT modify the original array by default. 3v4l.org/qU4Qo You have to explicitly tell it to modify the elements. This answer is misleading researchers.