Reputation: 108
Currently I have a file that looks something like this:
Awesomedude123 = 399,408 = September 16, 2012:
Username11 = 1,914,144 = September 16, 2012:
EpicSurfer = 1,031,427 = September 16, 2012:
What I want to do is transform it into a multidimensional array with PHP so it looks something like this:
Array
(
[1] => Array
(
[0] => Awesomedude123
[1] => 399,408
[2] => September 16, 2012
)
[2] => Array
(
[0] => Username11
[1] => 1,914,144
[2] => September 16, 2012
)
[3] => Array
(
[0] => EpicSurfer
[1] => 1,031,427
[2] => September 16, 2012
)
)
I have tried using array_shift, but it didn't work out. Any help would be HIGHLY appreciated!
Upvotes: 0
Views: 5871
Reputation: 5243
You can use a regular expression to split your string:
myarray = array();
$file = fopen("myfile",'r');
while (!feof($file)) {
$line = fgets($file);
preg_match("/(\w+) = (.+) = (.+)/",$line,$matches);
myarray[] = array($matches[1],$matches[2],$matches[3]);
}
Upvotes: 2
Reputation: 1153
Here is the code:
<?php
$data = file_get_contents('File.txt'); // Get the file content
$data = str_replace(array("\n", "\r"), '', $data); // Clear newline characters
$data = explode(':', $data); // Get each record by : at end of line
unset($data[count($data) - 1]); // Clear the last empty element
$final_array = array();
foreach($data AS $row){ // Loop the exploded data
$final_array[] = explode(' = ', $row); // Explode each row by Space=Space to each row of final_array
}
print_r($final_array);
?>
Upvotes: 2