0

I have a text file (id.txt) that looks like this:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
    [10] => 11
    [11] => 12
    [12] => 13
    [13] => 15
    [14] => 16
    [15] => 101
)

How can I load this text file as an array in PHP? I think I'd have to use file_get_contents() but I don't know how to use it and allow PHP to read it as an array.

user2898075
  • 79
  • 1
  • 3
  • 10

2 Answers2

2
preg_match_all("/\[(\d+)\] => (\d+)/",file_get_contents('id.txt'), $matches);
var_dump($matches[2]);
Jompper
  • 1,412
  • 9
  • 15
0

your answer for this question is probably something like what @Joni Salmi suggested.

But you really should store your array as a json string.

$jsonString = json_encode($my_array);
writeToFile($my_array);

/* then, to read: */
$storedJsonString = file_get_contents($file);
$myArray = json_decode($storedJsonString);
jeff
  • 13,055
  • 29
  • 78
  • 136