2

I have an array of objects, but I need to remove a similar objects by a few properties from them:

for example:

array(12) {
  [0]=>
  object(stdClass)#848 (5) {
    ["variant"]=>
    object(stdClass)#849 (4) {
      ["name"]=>
      string(8) "Alex"      
    }
    ["age"]=>
    int(10)
  }
  [1]=>
  object(stdClass)#851 (5) {
    ["variant"]=>
    object(stdClass)#852 (4) {
      ["name"]=>
      string(8) "Alex"
    }
    ["age"]=>
    int(10)
  }

How to make a one object in array for this ( if for example I need to compare only by a name property? )

Still have an issue with it.

Updated

I've create a new array of objects:

$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);

So what I need to do is to compare an object properties (variant->surnames and ages), if two objects has a similar age and variant->surname we need to remove the one of these objects.

A half of solution is:

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

print_r($tmp);

Unfortunatelly I need an old-style array of objects.

Smash
  • 513
  • 5
  • 23

1 Answers1

0

I've found an example.

<?php
 $a = array ( 
    0 => array ( 'value' => 'America', ), 
    1 => array ( 'value' => 'England', ),  
    2 => array ( 'value' => 'Australia', ), 
    3 => array ( 'value' => 'America', ), 
    4 => array ( 'value' => 'England', ), 
    5 => array ( 'value' => 'Canada', ), 
);

$tmp = array ();

foreach ($a as $row) 
    if (!in_array($row,$tmp)) array_push($tmp,$row);

print_r ($tmp);
?>

Quoted from here

Community
  • 1
  • 1
  • I dont have a very similar objects, I have an objects with 2 similar properties and 1 different. So I need to compare only by a these properties. – Smash Feb 26 '13 at 10:44
  • no, I'm not, but I did something that u can to watch, wait a lit bit – Smash Feb 26 '13 at 22:56
  • the problem is that I need an old-style array, not the array that was on start, just groupped by the fields I wrote. – Smash Feb 26 '13 at 22:58