1

I have the following array in PHP. Its an array of objects. I am trying to create an unique array from it.

array
  0 => 
    object(ElggAnnotation)[1144]
      protected 'attributes' => 
        array
          'id' => string '21705' (length=5)
          'owner_guid' => string '40468' (length=5)
          'access_id' => string '2' (length=1)
          'time_created' => string '1406098167' (length=10)
          'enabled' => string 'yes' (length=3)
      private 'valid' (ElggExtender) => boolean false
  1 => 
    object(ElggAnnotation)[1145]
      protected 'attributes' => 
        array
          'id' => string '21706' (length=5)
          'owner_guid' => string '28715' (length=5)
          'access_id' => string '2' (length=1)
          'time_created' => string '1406098168' (length=10)
          'enabled' => string 'yes' (length=3)
          'name' => string 'scheduled' (length=9)
          'value' => string 'yes' (length=3)
          'type' => string 'annotation' (length=10)
      private 'valid' (ElggExtender) => boolean false
  2 => 
    object(ElggAnnotation)[1146]
      protected 'attributes' => 
        array
          'id' => string '21707' (length=5)
          'owner_guid' => string '40468' (length=5)
          'access_id' => string '2' (length=1)
          'time_created' => string '1406104062' (length=10)
          'enabled' => string 'yes' (length=3)           
      private 'valid' (ElggExtender) => boolean false
  3 => 
    object(ElggAnnotation)[1147]
      protected 'attributes' => 
        array
          'id' => string '21708' (length=5)
          'owner_guid' => string '28715' (length=5)
          'access_id' => string '2' (length=1)
          'time_created' => string '1406104062' (length=10)
          'enabled' => string 'yes' (length=3)              
      private 'valid' (ElggExtender) => boolean false
  4 => 
    object(ElggAnnotation)[1148]
      protected 'attributes' => 
        array
          'id' => string '21709' (length=5)
          'owner_guid' => string '40468' (length=5)
          'access_id' => string '2' (length=1)
          'time_created' => string '1406104195' (length=10)
          'enabled' => string 'yes' (length=3)              
      private 'valid' (ElggExtender) => boolean false

So i need to create a unique array as per its element 'owner_guid' ...I tried array_unique method..but its didn't worked for me ...

How do it guys ? Any idea ?

4
  • whats the error message for using array_unique?? Commented Jul 23, 2014 at 10:42
  • @Torrezzzz No error message its getting blank Commented Jul 23, 2014 at 10:43
  • take a look at this : stackoverflow.com/questions/2426557/array-unique-for-objects Commented Jul 23, 2014 at 10:48
  • @Torrezzzz its seems to be very different what i am looking for... Commented Jul 23, 2014 at 10:59

1 Answer 1

1
$unique = array();

foreach($objects as $object) {
    $unique[$object->owner_guid] = $object;
}

var_dump($unique);
Sign up to request clarification or add additional context in comments.

3 Comments

@RIADev $unique will contain 1 object per owner_guid, you can get the unique owner_guid by array_keys($unique)
Hey that can be done so easily...but what my question by using owner_guid can i make the master array unique
@RIADev that's what this does ... try it, if it doesn't work post your expected output in the question

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.