Skip to content
Advertisement

Parse multidimensional array PHP

I have the following PHP array output:

Array
(
    [ON] => Array
        (
            [0] => Array
                (
                    [Name] => James Smith
                    [Claim/Policy Number] => 787878
                    [Province] => ON
                    [Internal code] => 6987
                    [Total (including tax)] => $ 5,299.97
                )

            [1] => Array
                (
                    [Name] => John Smith
                    [Claim/Policy Number] => 9999999
                    [Province] => ON
                    [Internal code] => 1234
                    [Total (including tax)] => $ 3,136.32
                )

            [2] => Array
                (
                    [Name] => John Smith1
                    [Claim/Policy Number] => 909090
                    [Province] => ON
                    [Internal code] => 2873
                    [Total (including tax)] => $ 1,996.81
                )
        )

    [AB] => Array
        (
            [4] => Array
                (
                    [Name] => John Smith
                    [Claim/Policy Number] => 545454
                    [Province] => AB
                    [Internal code] => 8909
                    [Total (including tax)] => $ 3,720.40
                )

            [7] => Array
                (
                    [Name] => John Smith1
                    [Claim/Policy Number] => 223343
                    [Province] => AB
                    [Internal code] => 1113
                    [Total (including tax)] => $ 3,567.89
                )

        )

)

I am trying to find the best way to format this into the below table structure:

<h2>AB</h2>
<table>
    <tr>
        <td>
        Name
        </td>
        <td>
        Claim/Policy Number
        </td>
        <td>
        Internal Code
        </td>
        <td>
        Total (including tax)
        </td>
    </tr>
    <!-- Repeat for all [AB] rows -->
    <tr>
        <td>
        [0] Name
        </td>
        <td>
        [0] Claim/Policy Number
        </td>
        <td>
        [0] Internal Code
        </td>
        <td>
        [0] Total (including tax)
        </td>
    </tr>
    <!-- Repeat for all [AB] rows -->
</table>
<h2>ON</h2>
<table>
    <tr>
        <td>
 ...           

I need the loop to be flexible enough to write a new table with header for each sub array.

What is the best way to loop through this array to output new tables and header for each sub array in the array?

Advertisement

Answer

You just need a foreach inside a foreach.

Working example:

<?php
$tables = [
    'ON' => [
        '0' => [
            'Name' => 'James Smith',
            'Claim/Policy Number' => '787878',
            'Province' => 'ON',
            'Internal code' => '6987',
            'Total (including tax)' => '$ 5,299.97',
        ],
        '1' => [
            'Name' => 'John Smith',
            'Claim/Policy Number' => '9999999',
            'Province' => 'ON',
            'Internal code' => '1234',
            'Total (including tax)' => '$ 3,136.32',
        ],
        '2' => [
            'Name' => 'John Smith1',
            'Claim/Policy Number' => '909090',
            'Province' => 'ON',
            'Internal code' => '2873',
            'Total (including tax)' => '$ 1,996.81',
        ],
    ],
    'AB' => [
        '4' => [
            'Name' => 'John Smith',
            'Claim/Policy Number' => '545454',
            'Province' => 'AB',
            'Internal code' => '8909',
            'Total (including tax)' => '$ 3,720.40',
        ],
        '7' => [
            'Name' => 'John Smith1',
            'Claim/Policy Number' => '223343',
            'Province' => 'AB',
            'Internal code' => '1113',
            'Total (including tax)' => '$ 3,567.89',
        ],
    ]
];
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <?php foreach($tables as $name => $table): ?>
    <h2><?= $name ?></h2>
    <table>
        <tr>
            <th>Name</th>
            <th>Claim/Policy Number</th>
            <th>Internal Code</th>
            <th>Total (including tax)</th>
        </tr>
        <?php foreach($table as $row): ?>
        <tr>
            <td><?= $row['Name'] ?></td>
            <td><?= $row['Claim/Policy Number'] ?></td>
            <td><?= $row['Internal code'] ?></td>
            <td><?= $row['Total (including tax)'] ?></td>
        </tr>
        <?php endforeach; ?>
    </table>
    <?php endforeach; ?>
</body>
</html>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement