phparraysmultidimensional-array

How to display a multidimensional array


I have this array similar to this:

$suppliers = array(
  'Utility Warehouse' => array('Gas' => array(0,0), 'Electricty' => array(0,0)),
  'British Gas' => array('Gas' => array(93,124), 'Electricty' => array(93,124)),
  'Eon' => array('Gas' => array(93,124), 'Electricty' => array(93,124))
);

How can I display the information as follows?

Utility Warehouse
Gas: 0-0 Electricity 0-0

British Gas
Gas: 93-134 Electricity: 93-134

Eon
Gas: 93-124 Electricity: 93-134

You can see how the displayed data corresponds to the data in the array. I've tried this:

foreach($suppliers as $a){
    echo $a[0];
}

But this does nothing.


Solution

  • <?php 
    
    foreach($suppliers as $supplier => $category) {
        echo $supplier . '<br />';
        foreach($category as $cat_name => $values_arr) {
            echo $cat_name . ': ' . implode('-', $values_arr) . '<br /><br />';
        }
    }
    
    ?>