Filter an array in PHP
<p>I'm trying to write a sort/filter script using an array filter function in PHP using the lambda syntax.</p>

<p>This achieves what I want to do, but it looks like it can be optimised:</p>

<pre><code>$offersReceivedProcessedByItem = array();
  foreach ($this-&gt;currentSessionUser-&gt;items as $item) {
    $receivedOffersOnItem = array();
    foreach ($offersReceived as $offerReceived) {
     if ($offerReceived-&gt;item-&gt;id === $item-&gt;id) {
       $receivedOffersOnItem[$offerReceived-&gt;id] = $offerReceived;
     }
    }
  offersReceivedProcessedByItem[$item-&gt;id] = $receivedOffersOnItem;
}
</code></pre>

<p>(for each item a user has, filter through the offersReceived array and add the offer to a new array if it has an equal id, if that makes sense?!).</p>

<p>Here's what I'm trying:</p>

<pre><code>$offersReceivedProcessedByItem = array();
foreach ($this-&gt;currentSessionUser-&gt;items as $item) {
   $receivedOffersOnItem = array_filter($item-&gt;offers, function($offer){
     return $offer-&gt;item-&gt;id === $item-&gt;id;
   });
   $offersReceivedProcessedByItem[$item-&gt;id] = $receivedOffersOnItem;
}
</code></pre>

<p>But i'm getting that $item is undefined, I expect it's closed scope so I tried passing $item as another argument but then i'm getting 'missing argument 2'.</p>

<p>I tried this using the relational approach built into Fuel but PHP eats too much memory and fails.</p>

<p>Any help? What am I getting wrong?</p>
