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:
array (size=2)
0 =>
array (size=2)
'id' => int 11268
'users' =>
array (size=2)
0 =>
array (size=1)
'user_id' => int 960781
1 =>
array (size=1)
'user_id' => int 960786
1 =>
array (size=2)
'id' => int 11267
'users' =>
array (size=2)
0 =>
array (size=1)
'user_id' => int 960783
1 =>
array (size=1)
'user_id' => int 902177
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)
if ($res = $stmt->get_result()) { // it gets user details
while($row=$res->fetch_assoc()) {
foreach ($matches as &$match) {
foreach ($match['users'] as &$user) {
if($user['user_id']==$row['id']) {
$user['details']=$row;
}
}
}
}
}
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?
Thanks very much