仅显示二维数组列中的唯一值[重复]

问题描述 投票:0回答:1

如何删除

foreach
语句中的重复值?

这是我的

$data
数组:

$data = [
    [
        'idx' => 1,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '0000-00-00',
    ],
    [
        'idx' => 2,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '0000-00-00',
    ],
    [
        'idx' => 3,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '0000-00-00',
    ],
    [
        'idx' => 4,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-01',
    ],
    [
        'idx' => 5,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-03',
    ],
    [
        'idx' => 6,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-05',
    ],
    [
        'idx' => 7,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-07',
    ],
    [
        'idx' => 8,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-09',
    ],
    [
        'idx' => 9,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '0000-00-00',
    ],
    [
        'idx' => 10,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-20',
    ],
    [
        'idx' => 11,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '0000-00-00',
    ],
    [
        'idx' => 12,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '0000-00-00',
    ],
    [
        'idx' => 13,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '0000-00-00',
    ],
    [
        'idx' => 14,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-22',
    ],
    [
        'idx' => 15,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-22',
    ],
    [
        'idx' => 16,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-22',
    ],
    [
        'idx' => 17,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-22',
    ],
    [
        'idx' => 18,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-22',
    ],
    [
        'idx' => 19,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-22',
    ],
    [
        'idx' => 20,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-22',
    ],
    [
        'idx' => 21,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-22',
    ],
    [
        'idx' => 22,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-22',
    ],
    [
        'idx' => 23,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-22',
    ],
    [
        'idx' => 24,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-22',
    ],
    [
        'idx' => 25,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-22',
    ],
    [
        'idx' => 26,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-22',
    ],
    [
        'idx' => 27,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-22',
    ],
    [
        'idx' => 28,
        'id' => 1,
        'mname' => 'test1',
        'hname' => 'test1',
        'bdate' => '2024-01-22',
    ],

代码

<?php
    foreach ($data as $row){
        if($row['bdate'] == '0000-00-00'){
            continue;
        }
        echo '<li>'.$row['bdate'].'</li>';
    }
?>

显示如下。

2024-01-01
2024-01-03
2024-01-05
2024-01-07
2024-01-09
2024-01-20
2024-01-22
2024-01-22
2024-01-22
2024-01-22
2024-01-22
2024-01-22
2024-01-22
2024-01-22
2024-01-22
2024-01-22
2024-01-22
2024-01-22
2024-01-22
2024-01-22
2024-01-22
2024-01-22
2024-01-22

但是我想这样显示:

2024-01-01
2024-01-03
2024-01-05
2024-01-07
2024-01-09
2024-01-20
2024-01-22

如何删除重复的相同日期值?

php arrays multidimensional-array duplicates
1个回答
2
投票

尝试使用关联数组。您可以跟踪已打印的日期。

$printed_dates = array();
foreach ($data as $row){
    if($row['bdate'] == '0000-00-00' || isset($printed_dates[$row['bdate']])){
        continue;
    }
    $printed_dates[$row['bdate']] = true;
    echo '<li>'.$row['bdate'].'</li>';
}
© www.soinside.com 2019 - 2024. All rights reserved.