Array:

$objects = array( (object)array('name'=>'Stiven','age'=>25,'variant'=>(object)array('surname'=>'Sigal')), (object)array('name'=>'Michael','age'=>30,'variant'=>(object)array('surname'=>'Jackson')), (object)array('name'=>'Brad','age'=>35,'variant'=>(object)array('surname'=>'Pit')), (object)array('name'=>'Jolie','age'=>35,'variant'=>(object)array('surname'=>'Pit')), ); echo "<pre>"; print_r($objects); 

How to delete objects that have the same variant->surname and age ? More precisely, not to delete, but to leave one of them, i.e. so that such identical elements were not in the array.

  • those. Only Pete or Jolie should remain in this array, it depends on the coding style / decision. - Smash
  • Let me ask what the syntax is, (object) - heleg
  • some newfangled tutorial .... - thunder
  • one
    injecting to object type - akalend
  • one
    @Heleg: type manipulation - Sergiks

5 answers 5

  1. sort by usort() using the usort() and its own variant->surname , which compares according to the fields variant->surname and age . Then objects with the same values ​​will definitely be in a row;
  2. run through array_filter() and ff, preserving statically previous values ​​of variant->surname and age , in order to return true only if the values ​​have changed.

Upd. for the lazy: a working example .

  • let's also do him a serizalize , so really .. for sure? Okay, just kidding, thanks for responding, I answered myself, only a new question arose. - Smash
  • Lazy to understand? Here is a working option - confused check. It turned out) - Sergiks

bluntly brute force:

  • run through the array, if the element is the first, then
  • copy item
  • check the presence of an element in the new array
  • if not, copy the item and run on.
  • if yes, then just run on
  • in conclusion, we do unset () of the original array if it is not needed

As a result, we will have an array without duplicates.

  • stupidly brute ... stupidly 8-shkoy. Anything you need to compare the properties of objects, and not the objects themselves? - Smash

array_unique () , array_walk () , array_map () , array_filter ()

    I apologize for the necropolis (I found it through Google)
    My solution to the same problem:

     /** * Вернёт только те объекты из массива $bad, которые уникальны по признаку $property * @param array $bad * @param string $property */ function array_object_unique($bad,$property) { foreach ($bad as $k1 => $a) { foreach ($good as $k2 => $g) { if ($a->$property == $g->$property) { continue(2); } } $good[] = $a; } return $good; } 

    Although the option of SW. @sergiks is also good, it was not possible to launch it in the context of a class.
    Ps By the way, the same algorithm suggested uv. @akalend

      I came to this decision:

       $tmp = array(); foreach ($objects as $item=>$object) { $tmp[$object->variant->surname][$object->age] = $object; } print_r($tmp); 

      Everything is good, but I don’t know how to convert it into the previous array. (I think ...) If anyone has any ideas, help me.

      • What is this decision if it is not a solution :)) - Sergiks
      • Well, then the question remains valid. - Smash