0

I have a table which contains the following:

enter image description here

I need to extract the data into one array which can contain only the date where there are no duplicate keys or values.

The first query returns:

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

I then use:

do {
    $Fields1[] = $row_Water1; 
} while ($row_Water1 = mysql_fetch_assoc($Water1));

The second query returns:

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

I then use:

do {
    $Fields2[] = $row_Water2; 
} while ($row_Water2 = mysql_fetch_assoc($Water2));

Both $Fields1 and $Fields2 contain my data ready to merge.

I then use $Water = array_unique (array_merge ($Fields1, $Fields2)); to try and create an array which contains no duplicate keys or values. When I run the script the new merged array contains:

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

My question is how can I produce the merged array to contain the following:

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

I have tried to do this so many ways without any success, can anyone see a way this could be done.

1
  • Your desired output array has an impossible structure. You cannot have two keys in the same level called SeqId. Only after correcting your desired output can we show you the best way to group your data (in SQL). Commented Aug 18, 2020 at 11:19

1 Answer 1

1

With the examples you've given, I can think up something like this:

$array = array();

foreach ($Fields1 as $row) {
    if (!isset($array[$row['UniqueID']])) {
        $array[$row['UniqueID']] = $row;
    } else {
        if (!is_null($row['WaterHot'])) {
            $array[$row['UniqueID']]['WaterHot'] = $row['WaterHot'];
        }

        if (!is_null($row['WaterCold'])) {
            $array[$row['UniqueID']]['WaterCold'] = $row['WaterCold'];
        }
    }
}

foreach ($Fields2 as $row) {
    if (!is_null($row['WaterHot'])) {
        $array[$row['UniqueID']]['WaterHot'] = $row['WaterHot'];
    }

    if (!is_null($row['WaterCold'])) {
        $array[$row['UniqueID']]['WaterCold'] = $row['WaterCold'];
    }
}

It's pretty straight forward, and merge the WaterHot and WaterCold fields, where the UniqueID column is the unique key.
The UniqueID column will also be the key of the merged array. If you don't want this, and want a numeric column, you'll need to use array_values on the resulting array.

Sign up to request clarification or add additional context in comments.

2 Comments

Blaatpraat, thanks for your very interesting reply. This sure does give me an array with the required data when I use "print_r ($array)". How can I now use that data to display each item in the rows and columns.
Blaatpraat,I have sorted out how to get the values in to my rows and columns but each row displays the same records. I am using foreach($array as $key => $value) { echo $array[$row['UniqueID']]['Room']; }. Am I doing this correctly?

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.