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.

4
  • 3
    That's a bad way to store data and now you're paying for it Commented Dec 26, 2013 at 17:32
  • 2
    Why do you have an array saved like that, you should save array to files with json_encode for example and then read with json_decode Commented Dec 26, 2013 at 17:33
  • try to store as json array. Then you can easily get back your actual array. Commented Dec 26, 2013 at 17:35
  • You're welcome: eval.in/83186 Commented Dec 26, 2013 at 17:36

2 Answers 2

2
preg_match_all("/\[(\d+)\] => (\d+)/",file_get_contents('id.txt'), $matches);
var_dump($matches[2]);
Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.