phparraysmultidimensional-arraytranspose

Print data from a multidimensional array in a transposed format


I have the multi dimensional array as follows, called from print_r($mArray);. How do I print the output like the sample below?

Array (
    [0] => Array (
        [pamount] => Array (
            [0] => 190
            [1] => 190
        )
        [psubject] => Array (
            [0] => A
            [1] => A
        )
        [ptype] => Array (
            [0] => Water
            [1] => Internet
        )
    )
)

Sample

Subject | Type    |Amount
A       | Water   |109
A       | Internet|109

Solution

  • try this:

    $index = 0;
    foreach($array['pamount'] as $key => $value){
       echo '<span>'.$value.'</span>
             <span>'.$array['psubject'][$index].'</span>
             <span>'.$array['ptype'][$index].'</span><br>';
       $index++;
    }