DCJones DCJones - 2 months ago 7
PHP Question

PHP looping through an array, not displaying the first records

This is a question concerning a solutions provided by @Blaatpraat which solved that part of my issue.

I now have an array which contains:

Array ( [NXLHR01011474021550] =>
Array ( [UniqueID] => NXLHR01011474021550 [Room] => 0101 [AuditBy] => navexdemo2 [AuditDate] => 2016-09-16 11:26:00 [SeqID] => SeqID1306 [Status] => 1 [WaterHot] => 67.0 [WaterCold] => 18.0 )
[NXLHR01021474021587] =>
Array ( [UniqueID] => NXLHR01021474021587 [Room] => 0102 [AuditBy] => navexdemo2 [AuditDate] => 2016-09-16 11:26:00 [SeqID] => SeqID1306 [Status] => 0 [WaterHot] => 65.0 [WaterCold] => 21.0 )
)


I am now tring to display each record in the array row by row.

My code is;

foreach ($array as $key => $item) {
echo $array[$row['UniqueID']]['Room']."\n";
echo $array[$row['UniqueID']]['AuditBy']."\n";
echo $array[$row['UniqueID']]['WaterHot']."\n";
echo $array[$row['UniqueID']]['WaterCold']."\n";
}


But the result is:
"0102 navexdemo2 65.0 21.0"
"0102 navexdemo2 65.0 21.0"
the same record twice.

Does anyone know why the first record in the array is not displayed.

I have had a lot of help from members and I was trying to avoid asking for help but I have tried so many ways to get this to work without success.

Again, I thank you for your time.

Answer

Could this be what you were going for?

<?php

    $array = [
        'NXLHR01011474021550'  => [
            'UniqueID'  => 'NXLHR01011474021550',
            'Room'      => '0101',
            'AuditBy'   => 'navexdemo2',
            'AuditDate' => '2016-09-16 11:26:00',
            'SeqID'     => 'SeqID1306',
            'Status'    => 1,
            'WaterHot'  => 67.0,
            'WaterCold' => 18.0,
        ],

        'NXLHR01021474021587'  => [
            'UniqueID'  => 'NXLHR01021474021587',
            'Room'      => '0102',
            'AuditBy'   => 'navexdemo2',
            'AuditDate' => '2016-09-16 11:26:00',
            'SeqID'     => 'SeqID1306',
            'Status'    => 0,
            'WaterHot'  => 65.0,
            'WaterCold' => 21.0,
        ],
    ];

    foreach($array as $uKey=>$arrData){     
        echo $arrData['Room']."\n";
        echo $arrData['AuditBy']."\n";
        echo $arrData['WaterHot']."\n";
        echo $arrData['WaterCold']."\n";

    }
Comments