Forum Moderators: coopster
FILE:
l0p0,l0p1,l0p2
l1p0,l1p1,l1p2
l2p0,l2p1,l2p2...
I want to use this information in a script, so I call the file using the following command:
$file=file("myfile.txt");
This breaks up the file into an array, with each key representing a different line.
I want to further break down this array efficiently as possible so that each line is exploded by the comma.
I'm wanting to get an output like this:
Array
(
[0] => Array
(
[0] => l0p0
[1] => l0p1
[2] => l0p2
)
[1] => Array
(
[0] => l1p0
[1] => l1p1
[2] => l1p2
)
[2] => Array
(
[0] => l2p0
[1] => l2p1
[2] => l2p2
)
)
I tried using this line: $file=explode(","file("myfile.txt")); but what it seems to be doing is just exploding the word "Array"
print_r $file[0][0] => "A"
print_r $file[0][1] => "r"
However, explode(",",$file[0]) seems to work.
How can I explode my array without having to do an explode command each time I want to use a value?
Thanks
$values = array();
foreach(file("myfile.txt") as $line => $content) {
$values[$line] = explode(',',$content);
}
echo '<pre>';
print_r($values);
echo '</pre>';
Good luck! :)