Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

How can I remove duplicate values from a multi-dimensional array in PHP?

Example array:

   Array
(
    [choice] => Array
        (
            [0] => Array
                (
                    [day] => Monday
                    [value] => Array
                        (
                            [0] => Array
                                (
                                    [name] => BI
                                    [time] => 10:00 
                                    [location] =>  B123
                                )
                            [1] => Array
                                (
                                    [name] => BI
                                    [time] => 11:00 
                                    [location] =>  A123
                                )
                        )
                )

            [1] => Array
                (
                    [day] => Tuesday
                    [value] => Array
                        (
                            [0] => Array
                                (
                                    [name] => BI
                                    [time] => 10:00 
                                    [location] =>  B123
                                )
                            [1] => Array
                                (
                                    [name] => BI
                                    [time] => 11:00 
                                    [location] =>  A123
                                )
                        )
                    )
        )
)

I'd like to remove those with duplicate name. So i only want to keep one subject each day.

my code so far:

$taken = array();
foreach($subject_list['choice'][0]["value"] as $key =>$item )
{ 
    if(!in_array($item['name'], $taken)) 
    {
        $taken[] = $item['name'];
    }else 
    {
        unset($flight_list['choice'][0]["value"][$key]);
    }

}

OUTPUT of the code above (which is obviously wrong):

Array
(
    [choice] => Array
        (
            [0] => Array
                (
                    [day] => Monday
                    [value] => Array
                        (
                            [0] => Array
                                (
                                    [name] => BI
                                    [time] => 10:00 
                                    [location] =>  B123
                                )
                        )
                )

            [1] => Array
                (
                    [day] => Tuesday
                    [value] => Array
                        (
                            [0] => Array
                                (
                                    [name] => BI
                                    [time] => 10:00 
                                    [location] =>  B123
                                )
                            [1] => Array
                                (
                                    [name] => BI
                                    [time] => 11:00 
                                    [location] =>  A123
                                )
                        )
                    )
        )
)

Anyone can help me how can i remove same class name at Tuesday.

share|improve this question
up vote 2 down vote accepted

If you want to keep the first set of unique values in each value batch in terms of name, then just create a temporary container for that. If you already have pushed it, then don't process anything, after the gathering, overwrite the batch using foreach with & reference:

foreach($subject_list['choice'] as &$items) {
    $temp = array(); // temporary container for current iteration
    foreach($items['value'] as $value) {
        if(!isset($temp[$value['name']])) { // if its new
            $temp[$value['name']] = $value; // push the batch using the key name
        }
    }
    $items['value'] = $temp; // apply unique value in the end of this batch
}

Sample Output

share|improve this answer

where $array is a php variable where your array is coming

 $array = array_map("unserialize", array_unique(array_map("serialize", $array)));
share|improve this answer

Just a quick google to remove duplicates from a multi dimensional array:

<?php
function super_unique($array)
{
  $result = array_map("unserialize", array_unique(array_map("serialize", $array)));

  foreach ($result as $key => $value)
  {
    if ( is_array($value) )
    {
      $result[$key] = super_unique($value);
    }
  }

  return $result;
}
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.