【问题标题】:print only last item of each duplicate in foreach仅打印 foreach 中每个重复项的最后一项
【发布时间】:2021-11-04 04:22:29
【问题描述】:

我有一个非常简单的数组,但有一些重复的条目。 我只想在每个重复项的末尾打印 cmets 列。我正在使用 php 框架 codeigniter v3。

请看下表结构:

下面是我迄今为止尝试过的代码,但它在第一项的开头打印。

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

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

编辑:

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' => '',
    
  ),

)

【问题讨论】:

  • 你能显示你的数组的var_export() 吗?
  • $value['Test_Comments '] !== $value['Test_Comments'] 注意尾随空格
  • @nice_dev 问题已更新。请参阅编辑
  • 嗯,它可能与大多数'Test_Comments' =&gt; '',有关
  • @RiggsFolly 不是所有的测试都有 cmets,只有少数有 cmets,你可以忽略空的 cmets

标签: php codeigniter foreach codeigniter-2


【解决方案1】:

您可以使用array_column 根据ID 键重新索引数组。对于每个 ID,它将始终保持最后一次出现,从而每次为您生成每个 ID 的最后一条评论。

您可以稍后在其上使用array_values() 来删除 ID 作为键。

<?php

$result= array_values(array_column($result, null, 'Test_ID'));
print_r($result);

【讨论】:

  • 这确实假设 OP 不只是匆忙将数据放在一起,但如果他们没有:)
  • @RiggsFolly 你能详细说明一下吗?
猜你喜欢
  • 2017-08-28
  • 1970-01-01
  • 2015-01-04
  • 2012-05-16
  • 1970-01-01
  • 2013-01-07
  • 2020-06-08
  • 1970-01-01
  • 2013-01-30
相关资源
最近更新 更多