-2

How to ignore duplicate value from array using foreach() ?

Array
(
    [0] => stdClass Object
        (
            [sa_uc_id] => 1
            [uc_um_id] => 1
            [ui_coverimage] => 1513351051.jpg
            [um_name] => Suresh Gyan Vihar University
        )

    [1] => stdClass Object
        (
            [sa_uc_id] => 2
            [uc_um_id] => 1
            [ui_coverimage] => 1513351051.jpg
            [um_name] => Suresh Gyan Vihar University
        )

    [2] => stdClass Object
        (
            [sa_uc_id] => 3
            [uc_um_id] => 1
            [ui_coverimage] => 1513351051.jpg
            [um_name] => Suresh Gyan Vihar University
        )

)
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98

2 Answers2

0

Here is another way. No intermediate variables are saved.

We used this to de-duplicate results from a variety of overlapping queries.

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));
Jaimin Vyas
  • 114
  • 5
  • this will not work in OP's case. take his input array and check. it's because complete sub-arrays are not identical. only one-or-two indexes of sub-arrays are identical. In this case He will lost usful info or nothing will happen at all – Alive to die - Anant Dec 27 '17 at 12:31
  • 1
    There you have to mention the ref of your answer if it's copied from somewhere.copied from https://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php – A.D. Dec 27 '17 at 12:34
  • yeah.. I forgot to mention it. – Jaimin Vyas Dec 27 '17 at 12:37
0

for nested array there the array_uniqe some time moderate the answer for multi-level because of there the list item sa_uc_id is unique.In above you have use array without sa_uc_id to use array_unique. If above answer not working then try this one...

$data_ar = array();//your array here;
$temp_ar = array();

foreach($data_ar as $value)
{
    $tempCheck = 0;
    foreach($temp_ar as $val)
    {
        if($val['uc_um_id'] == $val['uc_um_id']) // whatever the item value you want to check wheather its uc_um_id or um_name
        {
            $tempCheck = 1;
        }
    }
    if($tempCheck === 0)
    $temp_ar[] = $value;
}
var_dump($temp_ar);
A.D.
  • 2,352
  • 2
  • 15
  • 25