2014-07-23 1 views
1

J'ai le tableau suivant en PHP. C'est un tableau d'objets. J'essaie de créer un tableau unique à partir de celui-ci.Créer un tableau unique en fonction de l'un des éléments du tableau

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 

donc je dois créer un tableau unique par son élément 'owner_guid' ... J'ai essayé array_unique method..but son n'a pas fonctionné pour moi ...

Comment le faire les gars? Une idée ?

+0

quel est le message d'erreur pour utiliser array_unique ?? – Torrezzzz

+0

@Torrezzzz Aucun message d'erreur son vide –

+0

jetez un oeil à ceci: http://stackoverflow.com/questions/2426557/array-unique-for-objects – Torrezzzz

Répondre

1
$unique = array(); 

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

var_dump($unique); 
+0

Alors, quelle devrait être la sortie les autres éléments seront là ??? –

+0

@RIADev '$ unique' contiendra 1 objet par' owner_guid', vous pouvez obtenir l'unique 'owner_guid' par' array_keys ($ unique) ' – FuzzyTree

+0

Hey cela peut être fait si facilement ... mais quelle est ma question en utilisant owner_guid puis-je faire le tableau principal unique –

Questions connexes