Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove similar objects from array?
    primarykey
    data
    text
    <p>I have an array of objects, but I need to remove a similar objects by a few properties from them:</p> <p>for example:</p> <pre><code>array(12) { [0]=&gt; object(stdClass)#848 (5) { ["variant"]=&gt; object(stdClass)#849 (4) { ["name"]=&gt; string(8) "Alex" } ["age"]=&gt; int(10) } [1]=&gt; object(stdClass)#851 (5) { ["variant"]=&gt; object(stdClass)#852 (4) { ["name"]=&gt; string(8) "Alex" } ["age"]=&gt; int(10) } </code></pre> <p>How to make a one object in array for this ( if for example I need to compare only by a name property? )</p> <p>Still have an issue with it.</p> <p><strong>Updated</strong></p> <p>I've create a new array of objects:</p> <pre><code>$objects = array( (object)array('name'=&gt;'Stiven','age'=&gt;25,'variant'=&gt;(object)array('surname'=&gt;'Sigal')), (object)array('name'=&gt;'Michael','age'=&gt;30,'variant'=&gt;(object)array('surname'=&gt;'Jackson')), (object)array('name'=&gt;'Brad','age'=&gt;35,'variant'=&gt;(object)array('surname'=&gt;'Pit')), (object)array('name'=&gt;'Jolie','age'=&gt;35,'variant'=&gt;(object)array('surname'=&gt;'Pit')), ); echo "&lt;pre&gt;"; print_r($objects); </code></pre> <p>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.</p> <p>A half of <code>solution</code> is:</p> <pre><code>$tmp = array(); foreach ($objects as $item=&gt;$object) { $tmp[$object-&gt;variant-&gt;surname][$object-&gt;age] = $object; } print_r($tmp); </code></pre> <p>Unfortunatelly I need an old-style array of objects.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload