Iterate through multilevel array.

I have this array (below) that I need to iterate through to produce a table. The problem is that the heading of the table is actually buried deep in the array and I can’t seem to wrap my head around how to iterate through it.

Array:

[code]Zone 1Array
(
[rat] => Array
(
[0] => Array
(
[id] => 62
[z_id] => 7
[o_name] => SCU 29
[history_table] => HUB65CRAC1RAT
[type] => rat
)
[1] => Array
(
[id] => 64
[z_id] => 7
[o_name] => SCU 30
[history_table] => HUB65CRAC2RAT
[type] => rat
)

    )

[sat] => Array
    (
        [0] => Array
            (
                [id] => 63
                [z_id] => 7
                [o_name] => SCU 29
                [history_table] => HUB65CRAC1SAT
                [type] => sat
            )
        [1] => Array
            (
                [id] => 65
                [z_id] => 7
                [o_name] => SCU 30
                [history_table] => HUB65CRAC2SAT
                [type] => sat
            )

    )

[/code]

I need to produce a table like this:
SCU 29
[table] [tr] [td] [/td] [td]Zone ID[/td][td]Table[/td][/tr]
[tr] [td]RAT[/td] [td]7[/td][td]HUB65CRAC1RAT[/td][/tr]
[tr] [td]SAT[/td] [td]7[/td][td]HUB65CRAC1SAT[/td][/tr] [/table]
[b]

SCU 30
[/b][table] [tr] [td] [/td] [td]Zone ID[/td][td]Table[/td][/tr]
[tr] [td]RAT[/td] [td]7[/td][td]HUB65CRAC2RAT[/td][/tr]
[tr] [td]SAT[/td] [td]7[/td][td]HUB65CRAC2SAT[/td][/tr] [/table]

Hi there,

My first suggestion would be to look into re-organising that array so that it has the “o_level” instead of the type (first level of array) and then a level under each level for “rat” and “sat” with the individual items under each one. I say this because it is always best to construct arrays in the order that the details are needed - as it stands, the code could become messy or at least hard to navigate.

Let me know if this is possible or not - if it is, I believe the code will be much shorter and simpler.

Unfortunately, I inherited a very strange database. Each physical component object has it’s own table in the database.

My problem is that physically, I have an object(s) (SCU 29, SCU 30) with many components (RAT, SAT)

I have 1-3 devices per zone with 2 components each.

The best solution I can see is to generate an intermediate array with table rows, then output tables.
Assuming your array named $zone, here is the code:

[php]<?php
$table=array();

foreach($zone as $key1=>$z1){
foreach($z1 as $key2=>$z2){
$table[$z2[‘o_name’]].=’

’.strtoupper($z2[‘type’].’ ‘.$z2[‘z_id’]).’ ’.$z2[‘history_table’].’ ’;
}
}

foreach($table as $header=>$rows){
echo ‘

’.$header.’

’;
echo ‘’.$rows.’
Zone ID Table
’;
}
?>[/php]

Thank you! That will works really well for me.

I could not wrap my head around that.

Sponsor our Newsletter | Privacy Policy | Terms of Service