I want to print out information from a multidimensional array.
Here is my array structure:
// A two-dimensional array
$cars=array
(
array('service' => "Windows8",'host' => Sydney1,'state' => on),
array('service' => "unix",'host' => Newyork2,'state' => off),
array('service' => "Windows8",'host' => Singapore3,'state' => on),
array('service' => "unix",'host' => Tokyo4,'state' => off),
array('service' => "Windows8",'host' => Tokyo4,'state' => on),
);
I want to arrange the data to print out the following:
Data grouped by service: desired output:
Windows8 (3): Sydney1 (on), Singapore3 (on), Tokyo4 (on)
unix (2): Newyork2 (off), Tokyo4 (off)
Data grouped by host: desired output:
Sydney1 (1): Windows8 (on)
Newyork2 (1): unix (off)
Singapore3 (1): Windows8 (on)
Tokyo4 (2): unix (off), Windows8 (on)
Here is the code I have tried so far:
$arr = array();
foreach($cars as $key => $item)
{
$arr[$item['service_name']][$key] = $item;
}
echo '<pre>';
print_r($arr);
echo '</pre>';
$size = sizeof($arr);
$i=1;
foreach($arr as $key => $item)
{
echo $key;
}