Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I remove duplicate value in foreach statement?

How can I remove the duplicate values in the foreach statement?

Here is my $data array:

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Code

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

It displays like the following.

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

But what I want to display like this:

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

How can I remove duplicated same date value ?

>Solution :

Try using an associative array. You can keep track of the dates that have already been printed.

$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>';
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading