Which approach is "better" (in terms of optimization and complexity)
<p>Suppose that whe have some object, derived from these class instances</p>

<pre><code>class A
{
...
 private b //list of object of class B
}

class B
{
...
 private c //list of object of class C
}

class C
{
...
 private id
}
</code></pre>

<p>Now, somewhere in my code, I've got this situation</p>

<pre><code>function findId(array $idList)
{
 [...]
}
</code></pre>

<p>Where I have to find (for each element of <code>$idList</code>) if an element is contained into this object "cascade"</p>

<h1>First solution</h1>

<pre><code>//object initialization
foreach($a-&gt;getB() as $b)
{
 foreach($b-&gt;getC() as $c)
 {
  foreach($idList as $id)
  {
   if($id == $c-&gt;getId())
   {
    //do something an break the cycle
   }
  }
 }
}
</code></pre>

<h1>Second Solution</h1>

<pre><code>//object initialization

$idSet = array();
foreach($a-&gt;getB() as $b)
{
 foreach($b-&gt;getC() as $c)
 {
  $idSet[] = $c-&gt;getId();
 }
}
$idSet = array_unique($idSet);

foreach($idList as $id)
{
 if(array_search($id,$idSet) !== false)
 {
  [...]
 }
}
</code></pre>

<p>Which is better? There are some alternative methods for reach my goal?</p>

<h1>IMPORTANT</h1>

<p>There isn't better data representation. This because these objects are some database object (doctrine2)</p>
