8

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
  • 2,649
  • 10
  • 43
  • 64

5 Answers5

25

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');
Community
  • 1
  • 1
Josh
  • 8,082
  • 5
  • 43
  • 41
  • 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. – snake plissken Jan 22 '12 at 19:25
  • 1
    @Josh: That's an array of strings, not integers. –  Jan 22 '12 at 19:26
  • 2
    @Josh: You can simplify that loop with a simple call to `array_walk`: `$arr = array_walk('intval', $arr);`. –  Jan 22 '12 at 19:34
  • @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. – Josh Jan 22 '12 at 19:37
  • Xm my prob was in exmplode i put the space character instead of ','. With , it works. Thank tou for your time guys. – snake plissken Jan 22 '12 at 19:37
  • The array_walk thing didn't work for me with php 5.4, but array_map did: $arr = array_map('intval', $arr); – Riesling Sep 27 '12 at 13:23
  • @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. – Josh Sep 27 '12 at 14:34
  • This will return non-numeric values as well because of the use of intval. "" == 0 using intval – CommandZ May 04 '15 at 18:43
  • Beware! This wont work i PHP 5.3+! use `$arr = array_map('intval', $arr);` instead. – Ondrej Machulda Mar 14 '16 at 12:01
  • No @Josh, `array_walk()` does NOT modify the original array by default. https://3v4l.org/qU4Qo You have to explicitly tell it to modify the elements. This answer is misleading researchers. – mickmackusa Mar 21 '22 at 10:16
11
return array_map('intval', explode(",", '1,2,3,4,5,6,7,8,9'));
user2001487
  • 453
  • 4
  • 14
  • 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. – user2001487 Jul 10 '13 at 22:02
  • 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. – Roland Soós Aug 05 '13 at 07:00
4

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)
    }
    }
*/
CommandZ
  • 3,333
  • 1
  • 23
  • 28
3
explode(",",'1,2,3,4,5,6,7,8,9');

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

Oyeme
  • 11,088
  • 4
  • 42
  • 65
-3

You can use http://pl.php.net/manual/en/function.explode.php

Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65
  • 2
    Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – TimWolla Jan 22 '12 at 20:09