0

I have an object array that looks like this:

Array
(
[0] =>Object
        (
            [ClassScheduleID] => 2263
            [Name] => Workout 1
            [Location] => Object
            (
                [BusinessID] => 1
            )
)
[1] =>Object
        (
            [ClassScheduleID] => 2263
            [Name] => Workout 1
            [Location] => Object
            (
                [BusinessID] => 13
            )
)
[2] =>Object
        (
            [ClassScheduleID] => 2264
            [Name] => Workout 2
            [Location] => Object
            (
                [BusinessID] => 22
            )
)

I am looking to identify that the ClassScheduleID of 2263 is a duplicate, and remove the duplicate entry's entire object from the array. So that I get:

Array
    (
    [0] =>Object
            (
                [ClassScheduleID] => 2263
                [Name] => Workout 1
                [Location] => Object
                (
                    [BusinessID] => 1
                )
    )

    [1] =>Object
            (
                [ClassScheduleID] => 2264
                [Name] => Workout 2
                [Location] => Object
                (
                    [BusinessID] => 22
                )
    )

I tried the solution proposed here

How to remove duplicate values from a multi-dimensional array in PHP

but the count() remained the same

6
  • Why don't you use the class schedule id as the key? That way you can't have duplicates. Commented Apr 22, 2014 at 15:53
  • The solution you tried is for arrays that are identical. Yours are not, the BusinessID is different. Commented Apr 22, 2014 at 15:54
  • I am working with a defaulted output. If I want to change the key, then I need to rearrange the array myself. How would I do that? Commented Apr 22, 2014 at 15:55
  • Abra, I want to remove the entire node if the ClassScheduleID is the same, regardless of the rest Commented Apr 22, 2014 at 15:55
  • How do you determine which one to keep? The one with ->Location->BusinessID == 1. Commented Apr 22, 2014 at 16:00

2 Answers 2

1
$count = 0;
foreach($arrayObj as $key => $value) {
    if ($value['ClassScheduleID'] == 2263) {
        $count++;
    }
    if ($count > 1){
        unset($arrayObj[$key]);
        $count--;
    }
}

It works: http://ideone.com/fork/zrdDtu

Edit: Modified to delete any duplicates:

foreach($arrayObj as $key => $value) {
    $count = 0;
    foreach($arrayObj as $nkey => $nvalue) {
        if ($value['ClassScheduleID'] == $nvalue['ClassScheduleID']) {
            $count++;
        }
        if ($count > 1){
            unset($arrayObj[$key]);
            $count--;
        }
    }
}

var_dump($arrayObj);

See it here: http://ideone.com/fork/85RCst

Sign up to request clarification or add additional context in comments.

1 Comment

sorry, i guess i should have been more specific. i want it to remove ANY duplicate entries, the 2263 was just for demonstration
0
function get_unique_array($array)
{
   $result = array_map("unserialize", array_unique(array_map("serialize", $array)));

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

   return $result;
}

Demo: http://3v4l.org/WOTHi#v430

1 Comment

as with the previous example, it only removes complete duplicates in the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.