Reputation: 1688
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
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
Reputation: 7688
Rather have the array serialize
d or json_encode
d, inside that file.
Then, when reading it, just unserialize
or json_decode
in $list
Example:
$list = unserialize(file_get_contents(myfile.txt));
Upvotes: 3