title: PHP optimize nested loop
body: <p>I have an array of matches, each match has an ID and an array of users (from 2 to 4), each user is uniquely identified by it's own user_id. Example of the array:</p>
  
  <pre><code>array (size=2)
    0 =&gt; 
      array (size=2)
        'id' =&gt; int 11268
        'users' =&gt; 
          array (size=2)
            0 =&gt; 
              array (size=1)
                'user_id' =&gt; int 960781
            1 =&gt; 
              array (size=1)
                'user_id' =&gt; int 960786
    1 =&gt; 
      array (size=2)
        'id' =&gt; int 11267
        'users' =&gt; 
          array (size=2)
            0 =&gt; 
              array (size=1)
                'user_id' =&gt; int 960783
            1 =&gt; 
              array (size=1)
                'user_id' =&gt; int 902177
  </code></pre>
  
  <p>Now I want to add users details to the above array, so I do a query on the DB and I have this: (the row with id=n contains the details of user with user_id=n)</p>
  
  <pre><code>if ($res = $stmt-&gt;get_result()) { // it gets user details
      while($row=$res-&gt;fetch_assoc()) {
          foreach ($matches as &amp;$match) {
              foreach ($match['users'] as &amp;$user) {
                  if($user['user_id']==$row['id']) {
                      $user['details']=$row;
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>This is working fine, but it's not the best way, because for each row I walk all the array. Do you have any idea how can I optimize this?</p>
  
  <p>Thanks very much</p>