简体   繁体   中英

PHP how to explore a multidimentional array

I have read the advanced help, sorry but I don't understand this editor!!!

I have created a multidimensional like:

[YYY] => Array
     (
         [0] => 11.12
         [1] => 22.775000000
         [2] => 33.895
         [AABB] => Array
             (
                 [0] => 22.12
                 [1] => 12.23000000
                 [2] => 23.895
             )
     )
[ZZZ] => Array
     (
         [0] => 1.12
         [1] => 33.775000000
         [2] => 44.895
         [NNNN] => Array
             (
                 [0] => 23.30
                 [1] => .000000000
                 [2] => 997.3
             )

The YYY and ZZZ are the main 'records. YYY does have a subrecord AABB and ZZZ does have a subrecord NNNN. In this example they have only one sub record but every main record can have one or more subrecords.

The result should be like this (sorry I don't understand the editor although if I read the advanced help it should work with |s and -s???):

  <table> <tr> <th> kolom 1</th><th>Kolom 2</th><th>Kolom 4</th><th>Kolom 4</td> </tr> <tr> <td>YYY</td><td>11.12</td><td>22.775000000</td><td>33.895</td> </tr> <tr> <td>- AABB</td><td>22.12</td><td>12.23000000</td><td>23.895</td> </tr> <tr> <td>ZZZ</td><td>1.12</td><td>33.775000000</td><td>44.895</td> </tr> <tr> <td>- NNNN</td><td>23.30</td><td>.000000000</td><td>997.3</td> </tr> </table> 

I hope that is clear what I want. Sorry for layout but again I don't understand how this editor works.

I'm trying todo with a foreach loop like (I have simplified it, to make it more clear):

foreach($aMarktWaardeExposure as $sLevel3 => $aLevel4) { 
        echo '<td><strong>'.$sLevel3.'</strong></td>'; //main record
        foreach($aLevel4 as $sLevel4) { 
                echo $sLevel4.'<br />'; //Sub record
        } 
}

I hope that someone can help me out.

Thanks,

Nico

Maybe something like this:

    <?php 

$array = [
    'YYY' => [
        0 => 11.12,
        1 => 22.775000000,
        2 => 33.895,
        'AABB' => [
            0 => 22.12,
            1 => 12.23000000,
            2 => 23.895,

        ]
    ], 
    'ZZZ' => [
        0 => 1.12,
        1 => 33.775000000,
        2 => 44.895,
        'NNNN' => [
            0 => 23.30,
            1 => .000000000,
            2 => 997.3
        ]
    ]
];
?> 
<style type="text/css">
table td{
    border: 1px solid red;
}
</style>

<table>
    <?php foreach($array as $row): ?>
        <tr>
            <?php foreach($row as $cell): ?>
                <td>
                    <?= is_array($cell) ? implode('<br>', $cell) : $cell ?>
                </td>
            <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
</table>

Output:

    <table>
    <tbody>
        <tr>
            <td>11.12</td>
            <td>22.775</td>
            <td>33.895</td>
            <td>22.12<br>12.23<br>23.895</td>
        </tr>
        <tr>
            <td>1.12</td>
            <td>33.775</td>
            <td>44.895</td>
            <td>23.3<br>0<br>997.3</td>
        </tr>
    </tbody>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM