PHP 优化嵌套循环
PHP optimize nested loop
我有一组匹配项,每个匹配项都有一个 ID 和一组用户(从 2 到 4),每个用户都由它自己的 user_id 唯一标识。数组示例:
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
现在我想将用户详细信息添加到上面的数组中,所以我在数据库上进行了查询,并且得到了这个:(id=n 的行包含 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;
}
}
}
}
}
这工作正常,但这不是最好的方法,因为我对每一行都遍历了整个数组。你知道我该如何优化它吗?
非常感谢
您可以通过按 userid 索引用户数组来简化问题。现在代码变得有点复杂,但是计算复杂度class 更低了。如果新解决方案真的更快,则取决于各个阵列的实际大小,因此您必须使用实际生产数据来衡量这两种解决方案,以了解实际上最快的解决方案是什么。
<?php
function index_array_by($array, $key) {
$result = array();
foreach($array as &$value) {
$new_key = $value[$key];
$result[$new_key] = &$value;
}
return $result;
}
foreach($matches as &$match) {
$match['users'] = index_array_by($match['users'], "user_id");
}
if ($res = $stmt->get_result()) { // it gets user details
while($row=$res->fetch_assoc()) {
foreach ($matches as &$match) {
$user_id = $row['id'];
$match['users'][$user_id]['details'] = $row;
}
}
}
?>
我发现这个解决方案只需要扫描数组一次,但我猜它使用了更多内存,因为我将行保存到数组中:
//Save the results in an array to later add to the matches array
if ($res = $stmt->get_result()) { //second get: it gets user details
while($row=$res->fetch_assoc()) {
$rows[$row['id']]=$row;
}
}
mysqli_free_result($res);
//Add the user details to the matches array
foreach ($matches as &$match) {
foreach ($match['users'] as &$user) {
$user['details']=$rows[$user['user_id']];
}
}
我有一组匹配项,每个匹配项都有一个 ID 和一组用户(从 2 到 4),每个用户都由它自己的 user_id 唯一标识。数组示例:
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
现在我想将用户详细信息添加到上面的数组中,所以我在数据库上进行了查询,并且得到了这个:(id=n 的行包含 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;
}
}
}
}
}
这工作正常,但这不是最好的方法,因为我对每一行都遍历了整个数组。你知道我该如何优化它吗?
非常感谢
您可以通过按 userid 索引用户数组来简化问题。现在代码变得有点复杂,但是计算复杂度class 更低了。如果新解决方案真的更快,则取决于各个阵列的实际大小,因此您必须使用实际生产数据来衡量这两种解决方案,以了解实际上最快的解决方案是什么。
<?php
function index_array_by($array, $key) {
$result = array();
foreach($array as &$value) {
$new_key = $value[$key];
$result[$new_key] = &$value;
}
return $result;
}
foreach($matches as &$match) {
$match['users'] = index_array_by($match['users'], "user_id");
}
if ($res = $stmt->get_result()) { // it gets user details
while($row=$res->fetch_assoc()) {
foreach ($matches as &$match) {
$user_id = $row['id'];
$match['users'][$user_id]['details'] = $row;
}
}
}
?>
我发现这个解决方案只需要扫描数组一次,但我猜它使用了更多内存,因为我将行保存到数组中:
//Save the results in an array to later add to the matches array
if ($res = $stmt->get_result()) { //second get: it gets user details
while($row=$res->fetch_assoc()) {
$rows[$row['id']]=$row;
}
}
mysqli_free_result($res);
//Add the user details to the matches array
foreach ($matches as &$match) {
foreach ($match['users'] as &$user) {
$user['details']=$rows[$user['user_id']];
}
}