3
我有两个数组PHP的:数组嵌套循环性能
$list = Array
([0] => stdClass Object
(
[id] => 10
[data] => "test data"
)
[1] => stdClass Object
...
...(max 3000 ~ 4000 items)
和
$attributes = Array
([0] => stdClass Object
(
[ids] => 11
[list] => '<ul>...</ul>'
)
[1] => stdClass Object
...
...(max 3000 ~ 4000 items)
我试图离开加入他们的行列,但仅在执行方式,我能写出这是可用是
$nrrowslist = count($list);
for ($i = 0; $i < $nrrowslist; $i++){
$nrat = count($attributes);
for ($j = 0; $j < $nrat; $j++)
{
if($list[$i]->id == $attributes[$j]->ids){
$list[$i]->attributes = $attributes[$j]->list;
array_splice($attributes, $j, 1); // remove the item
break; // since there is other item with that id
}
}
}
//完成在〜0.470秒
但如果我把它写
foreach($list as $art){
foreach($attributes as $attr){
if($art->id == $attr->ids){
$art->attributes = $attr->list;
}
}
}
它完成了〜5.500秒..和太多
我能做些什么来执行更第一种方法的情况呢?
它运行得更快。谢谢! (〜0.280秒) – daniel
好消息! – Scuzzy