0

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.

7
  • 1
    $row['UniqueID'] is always the same Commented Sep 17, 2016 at 14:23
  • @u_mulder hi, when you say "$row['UniqueID'] is always the same" are you asking if it is or are you saying it always will be. The "UniqueID changes depending on the record. Commented Sep 17, 2016 at 14:25
  • Depending on what record? In your foreach it is always the same. Commented Sep 17, 2016 at 14:27
  • 1
    UniqueID is a key if $item, not $row. Commented Sep 17, 2016 at 14:30
  • 1
    Use $item instead of $row inside the loop, is that I think what @u_mulder is saying. Commented Sep 17, 2016 at 14:30

2 Answers 2

2

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";

    }

To visualise the Output a little Clearer, you could take this route:

<?php

    $dataList   = "<ul>" . PHP_EOL;
    foreach($array as $uKey=>$arrData){
        $sp        = "&nbsp;&nbsp;&nbsp;&nbsp;";
        $dataList .= "<li><h3>Unique ID: {$uKey}</h3></li>"                             . PHP_EOL;
        $dataList .= "<li><strong>Room: </strong>{$sp}{$arrData['Room']}</li>"          . PHP_EOL;
        $dataList .= "<li><strong>AuditBy: </strong>{$sp}{$arrData['AuditBy']}</li>"    . PHP_EOL;
        $dataList .= "<li><strong>WaterHot: </strong>{$sp}{$arrData['WaterHot']}</li>"  . PHP_EOL;
        $dataList .= "<li><strong>WaterCold: </strong>{$sp}{$arrData['WaterCold']}</li>". PHP_EOL;
    }
    $dataList .= "</ul>";

    echo $dataList;

The echo $dataList above would produce something like:

  • Unique ID: NXLHR01011474021550

  • Room:              0101
  • AuditBy:          navexdemo2
  • WaterHot:        67
  • WaterCold:     18

  • Unique ID: NXLHR01021474021587

  • Room:             0102
  • AuditBy:         navexdemo2
  • WaterHot:       65
  • WaterCold:     21
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

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

2 Comments

I used foreach ($array as $key => $item) { echo $item['Room']."\n"; echo $item['AuditBy']."\n"; echo $item['WaterHot']."\n"; echo $item['WaterCold']."\n"; } and wored just as I needed.
Cool.. happy coding :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.