我有两个数组
$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秒才能完成。..
我该怎么做才能执行更多第一种方法?
您可以通过两次迭代并在分配中使用common参数获得一些成功
$newlist = array();
// I'm sure there's a better way to do this initial assignment
foreach($list as $row)
{
$newlist[$row->id] = $row;
}
foreach($attributes as $attr)
{
if(isset($newlist[$attr->ids]) === true)
{
$newlist[$attr->ids]->attributes = $attr->list;
}
}
var_dump($newlist);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句