Need help with reading log file into Multi-dimensional associative array

Ok I have a basic understanding of Multi-dimensional arrays and Associative array but im totally lost on if this is possible. I need to take a log file that is somewhat delimited and read it into an array. here is an example of the text file. The groups are separated by a blank line in the log and each heading/value is separated by a ; on seperate lines the heading is on one the value is on the next line.

System Summary

Host Name;System Location;Life Cycle Controller
PHLENEL;Please set the value;[N/A]

Chassis Model;Chassis Service Tag;Chassis Lock;Chassis Asset Tag
PowerEdge R200;4TFJ5H1;Present;

Processor Brand;Processor Version;Voltage
Intel® Xeon® CPU X3350 @ 2.66GHz;Model 23 Stepping 7;1400 mV

Total Installed Capacity;Memory Available to the OS;Total Maximum Capacity;Memory Array Count
4096 MB;4032 MB;8192 MB;1

Location;Use;Installed Capacity;Maximum Capacity;Slots Available;Slots Used;ECC Type
System Board or Motherboard;System Memory;4096 MB;8192 MB;4;4;Multibit ECC

What I want is a way to read the log into an array that looks basically like this

Array
(
[0] => Array
(
[Host Name] => PHLENEL
[System Location] => Please set the value
[Life Cycle Controller] => [N/A]
)
[1] => Array
(
[Chassis Model] => PowerEdg R200
[Chassis service Tag] => 4TFJ5H1
[Chassis Lock] => Present
[Chassis Asset Tag] => NULL
)
[2] => Array
(
[Processor Brand] => Intel® Xeon® CPU X3350 @ 2.66GHz
[Processor Version] => Model 23 Stepping 7
[Voltage] => 1400mV
)
And continue on like that for the rest of the log file. My main issue is how to read that log file into that array setup. Any help on this would be great or even an idea of what might have to be done so I have a good starting place to look at.

Hi there,

Just knocked this up quickly, there might be shorter ways of doing it but this works for me:
[php]function onespace($string)
{
$newstring = preg_replace("/\s+/"," “,$string);
$newstring = preg_replace(”/\t+/"," ",$newstring);
return $newstring;
}

$file = file(“mdano01.txt”);

echo ‘

’.$file[0].’

’;
unset($file[0]);

foreach($file as $key => $line)
{
if(trim($line) == “”) unset($file[$key]);
}

$file = array_values($file);

$oput = array();

$k=-1;
for($i=0;$i<count($file);$i=$i+2)
{
if($i%2==0)
{
$k++;
$data1 = explode(";",$file[$i]);
$data2 = explode(";",$file[$i+1]);
foreach($data1 as $key => $data)
{
$oput[$k][trim($data)] = trim(onespace($data2[$key]));
}
}

}

echo “

”;
print_r($oput);
echo “
”;
[/php]

Remove the onespace function if you want, I just noticed that you had quite a few big gaps, so I chucked that in there just in case you wanted it.

Smokey,

Thank you that is exactly what I needed and it works perfectly for the entire log file. The onespace function is great also because when the system generates the log file it has a lot of white space in it.

Again that you for the help. Great Job.

It was my pleasure. By the way this interested me so I challenged myself to shorten the code, if you want the shorter version I have put it below (haven’t tested speed differences by the way):

[php]function onespace($string)
{
$newstring = preg_replace("/\s+/"," “,$string);
$newstring = preg_replace(”/\t+/"," ",$newstring);
return $newstring;
}

$file = file_get_contents(“mdano01.txt”);
$file = explode("\r\n\r\n",$file);

echo ‘

’.$file[0].’

’;
unset($file[0]);

foreach($file as $key => $line)
{
$parts = explode("\r\n",$line);
$file[$key] = explode(";",$parts[0]);
$file2[$key] = explode(";",$parts[1]);
}

$oput = array();
foreach($file as $gkey => $info)
{
foreach($info as $hkey => $val)
{
$oput[$gkey][$val] = onespace($file2[$gkey][$hkey]);
}
}

echo “

”;
print_r($oput);
echo “
”;
[/php]

It’s not so much shorter, come to think of it - but it’s a different way of doing it… !

Sponsor our Newsletter | Privacy Policy | Terms of Service