-1

I have a very simple array but with some duplicate entries. I just want to print the comments column at the end of each duplicate item. I am using php framework codeigniter v3.

Please see the following table structure:

enter image description here

below is the code i have tried so far but it is printing at the start of the first item.

    $Test_Comments = "";
    foreach($result as $value)
    {
        if ($Test_Comments != $value['Test_Comments'])
        {
            echo $value['Test_Comments'];
        }

        $Test_Comments = $value['Test_Comments'];
    }

EDIT:

var_export($result);
array (
  0 => 
  array (
    'ID' => '570',
    'Patient_ID' => '4558',
    'Test_ID' => '570',
    'Test_Status' => 'Performed',
    'Head_ID' => '4769',
    'Result_Value' => 'Negative',
    'Test_Name' => 'Viral Marker ',
    'Test_Comments' => '<p><strong><em>Comments:</em></strong></p>
<p><em>Anti HCV,HbsAg were performed by immunochoromatographic Screeing Method. The Technique has Sensitivity of 99% and Sepcifity of 98% .Clinically&nbsp; inconsistent result should be reconfrimed by alternative method e.g (ELISA,PCR)</em></p>',
    
  ),
  1 => 
  array (
    'ID' => '570',
    'Patient_ID' => '4558',
    'Test_ID' => '570',
    'Test_Status' => 'Performed',
    'Head_ID' => '4769',
    'Result_Value' => 'Negative',
    'Test_Name' => 'Viral Marker ',
    'Test_Comments' => '<p><strong><em>Comments:</em></strong></p>
<p><em>Anti HCV,HbsAg were performed by immunochoromatographic Screeing Method. The Technique has Sensitivity of 99% and Sepcifity of 98% .Clinically&nbsp; inconsistent result should be reconfrimed by alternative method e.g (ELISA,PCR)</em></p>',
    
  ),
  2 => 
  array (
    'ID' => '464',
    'Patient_ID' => '4558',
    'Test_ID' => '464',
    'Test_Status' => 'Performed',
    'Head_ID' => '4769',
    'Result_Value' => '76',
    'Test_Name' => 'Random Sugar',
    'Test_Comments' => '',
    
  ),
  3 => 
  array (
    'ID' => '340',
    'Patient_ID' => '4558',
    'Test_ID' => '340',
    'Test_Status' => 'Performed',
    'Head_ID' => '4769',
    'Result_Value' => '0.7',
    'Test_Name' => 'LFT-Liver Functions Tests',
    'Test_Comments' => '',
    
  ),
  4 => 
  array (
    'ID' => '340',
    'Patient_ID' => '4558',
    'Test_ID' => '340',
    'Test_Status' => 'Performed',
    'Head_ID' => '4769',
    'Result_Value' => '0.5',
    'Test_Name' => 'LFT-Liver Functions Tests',
    'Test_Comments' => '',
    
  ),
  5 => 
  array (
    'ID' => '340',
    'Patient_ID' => '4558',
    'Test_ID' => '340',
    'Test_Status' => 'Performed',
    'Head_ID' => '4769',
    'Result_Value' => '0.2',
    'Test_Name' => 'LFT-Liver Functions Tests',
    'Test_Comments' => '',
    
  ),
  6 => 
  array (
    'ID' => '340',
    'Patient_ID' => '4558',
    'Test_ID' => '340',
    'Test_Status' => 'Performed',
    'Head_ID' => '4769',
    'Result_Value' => '29',
    'Test_Name' => 'LFT-Liver Functions Tests',
    'Test_Comments' => '',
    
  ),
  7 => 
  array (
    'ID' => '340',
    'Patient_ID' => '4558',
    'Test_ID' => '340',
    'Test_Status' => 'Performed',
    'Head_ID' => '4769',
    'Result_Value' => '32',
    'Test_Name' => 'LFT-Liver Functions Tests',
    'Test_Comments' => '',
    
  ),
  8 => 
  array (
    'ID' => '340',
    'Patient_ID' => '4558',
    'Test_ID' => '340',
    'Test_Status' => 'Performed',
    'Head_ID' => '4769',
    'Result_Value' => '164',
    'Test_Name' => 'LFT-Liver Functions Tests',
    'Test_Comments' => '',
    
  ),
  9 => 
  array (
    'ID' => '124',
    'Patient_ID' => '4558',
    'Test_ID' => '124',
    'Test_Status' => 'Performed',
    'Head_ID' => '4769',
    'Result_Value' => '0.7',
    'Test_Name' => 'RFT-Renal Function Tests',
    'Test_Comments' => '',
    
  ),
  10 => 
  array (
    'ID' => '124',
    'Patient_ID' => '4558',
    'Test_ID' => '124',
    'Test_Status' => 'Performed',
    'Head_ID' => '4769',
    'Result_Value' => '23',
    'Test_Name' => 'RFT-Renal Function Tests',
    'Test_Comments' => '',
    
  ),

)
Shafat Ahmad
  • 77
  • 2
  • 14

1 Answers1

0

You can use array_column to re-index the array according to the ID key. It will always keep last occurrence with it for each ID yielding you the last comment for each ID each time.

You can later on operate with array_values() on it to remove IDs as keys.

<?php

$result= array_values(array_column($result, null, 'Test_ID'));
print_r($result);
nice_dev
  • 17,053
  • 2
  • 21
  • 35