'unable to echo multidimensional array in php

I am getting a json server response and i have used json decode function to convert json into array. $arr = json_decode($json, true); The result is as follows :

Array

(
    [ShipmentData] => Array
        (
            [0] => Array
                (
                    [Shipment] => Array
                        (
                            [PickUpDate] => 2022-02-07T19:46:34
                            [Destination] => Surat
                            [DestRecieveDate] => 2022-02-11T08:52:07.061000
                            [Scans] => Array
                                (
                                    [0] => Array
                                        (
                                            [ScanDetail] => Array
                                                (
                                                    [ScanDateTime] => 2022-02-05T09:34:27.847000
                                                    [ScanType] => UD
                                                    [Scan] => Manifested
                                                    [StatusDateTime] => 2022-02-05T09:34:27.847000
                                                    [ScannedLocation] => City (Maharashtra)
                                                    [Instructions] => Shipment details manifested
                                                    [StatusCode] => X-UCI
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [ScanDetail] => Array
                                                (
                                                    [ScanDateTime] => 2022-02-05T09:34:33
                                                    [ScanType] => UD
                                                    [Scan] => Manifested
                                                    [StatusDateTime] => 2022-02-05T09:34:33
                                                    [ScannedLocation] => City (Maharashtra)
                                                    [Instructions] => Pickup scheduled
                                                    [StatusCode] => FMPUR-101
                                                )

                                        )

                                    [2] => Array
                                        (
                                            [ScanDetail] => Array
                                                (
                                                    [ScanDateTime] => 2022-02-05T15:00:57
                                                    [ScanType] => UD
                                                    [Scan] => Manifested
                                                    [StatusDateTime] => 2022-02-05T15:00:57
                                                    [ScannedLocation] => City (Maharashtra)
                                                    [Instructions] => Out for Pickup
                                                    [StatusCode] => FMOFP-101
                                                )

                                        )

                                    [3] => Array
                                        (
                                            [ScanDetail] => Array
                                                (
                                                    [ScanDateTime] => 2022-02-05T15:00:58
                                                    [ScanType] => UD
                                                    [Scan] => Manifested
                                                    [StatusDateTime] => 2022-02-05T15:00:58
                                                    [ScannedLocation] => City (Maharashtra)
                                                    [Instructions] => Out for Pickup
                                                    [StatusCode] => FMOFP-101
                                                )

                                        )

                                    [4] => Array
                                        (
                                            [ScanDetail] => Array
                                                (
                                                    [ScanDateTime] => 2022-02-05T22:08:35
                                                    [ScanType] => UD
                                                    [Scan] => Manifested
                                                    [StatusDateTime] => 2022-02-05T22:08:35
                                                    [ScannedLocation] => City (Maharashtra)
                                                    [Instructions] => Pickup not attempted
                                                    [StatusCode] => FMEOD-106
                                                )

                                        )
                )

        )

)

Now i am trying to print data into html table using foreach loop but unable to do so. Here is my complete php code :

$json = file_get_contents('https://track.delhivery.com/api/v1/packages/json/?waybill=<THEWAYBILLNUMBER>&token=<THETOKENKEY>');
$arr = json_decode($json, true);
$items = $arr['ShipmentData'];
echo '<table>';
foreach ($items as $item) {
    foreach ($item['Shipment']['Scans']['ScanDetail'] as $step) {
        echo '<tr>';
        echo '<td>' . $step['ScanDateTime'] . '</td>';
        echo '<td>' . $step['Scan'] . '</td>';
    }
    echo '</tr>';
}
echo '</table>';
?>

Getting the following errors :

Warning: Undefined array key "Shipment" in index.php on line 25

Warning: Trying to access array offset on value of type null in index.php on line 25

Warning: Trying to access array offset on value of type null in index.php on line 25

Warning: foreach() argument must be of type array|object, null given in index.php on line 25


Solution 1:[1]

Scans is an array so $item['Shipment']['Scans']['ScanDetail'] will not work. You need another foreach to iterate over it. Try this

echo '<table>';
foreach ($items as $item) {
    foreach ($item['Shipment']['Scans'] as $ScanDetail=>$scans) {
        foreach ($scans as $step) {
            echo '<tr>';
            echo '<td>' . $step['ScanDateTime'] . '</td>';
            echo '<td>' . $step['Scan'] . '</td>';
            echo '</tr>';
        }
    }
}
echo '</table>';

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ruleboy21