user2511140
user2511140

Reputation: 1688

How use array in text file text file (PHP)?

I have a txt file that contains an array like this:

array (
  0 => 'jack',
  1 => 'alex'
)

I want to use this array in my PHP code but it is not working as on this example which returns a

$list=  file_get_contents(myfile.txt); 
echo $list[0];

How can I convert it ?

Upvotes: 0

Views: 49

Answers (2)

exussum
exussum

Reputation: 18550

To answer the quextion - eval looks like it will handle the array (Array has been output with var_export ?)

BUT exec'ing unknown code is a bad idea, W0rldart has the better ways to do it (json would be my preference)

Upvotes: 0

Alex
Alex

Reputation: 7688

Rather have the array serialized or json_encoded, inside that file. Then, when reading it, just unserialize or json_decode in $list

Example:

$list = unserialize(file_get_contents(myfile.txt));

Upvotes: 3

Related Questions