gpt4 book ai didi

php - 使用多个嵌套循环的最佳方式

转载 作者:可可西里 更新时间:2023-10-31 23:18:43 26 4
gpt4 key购买 nike

我为此使用 php,但也欢迎非特定语言的答案。

我有几个对象数组,我想循环遍历并按顺序输出。每个数组中都有不同种类的对象,但所有对象都具有唯一的顺序属性。

例如:

$people = [{'name':'George','email':'George@test.com','order':'2'},{'name...];
$sandwiches = [{'type':'bacon','rating':'8/10','order':'1'},{'type...];
$restaurants = ....
$chefs = ...
...

按顺序遍历它们的最有效方法是什么?

假设我可以确定我认为我可以做的最大订单:

for($i=0; $i< $maximumOrder; $i++)
{
for($j=0; $j< count($people); $j++)
{
if($people[$j]->order == $i)
{
//Do the things I want to do
break;
}
}


for($j=0; $j< count($sandwiches); $j++)
{
if($sandwiches[$j]->order == $i)
{
//Do the things I want to do
break;
}
}


for($j=0; $j< count($restaurants); $j++)
{
.....


}

但这不是很好,因为即使在 people 中找到具有所需顺序的项目,它仍会继续循环遍历所有其他数组。我可以只添加一个 bool 变量来显示是否已经找到所需的项目(见下文),但我相信有更好的方法可以做到这一点。

    for($i=0; $i< $maximumOrder; $i++)
{
$found = false;

for($j=0; $j< count($people); $j++)
{
if($people[$j]->order == $i)
{
//Do the things I want to do
$found = true;
break;
}
}

if(!$found == true)
{
for($j=0; $j< count($sandwiches); $j++)
{
if($sandwiches[$j]->order == $i)
{
//Do the things I want to do
$found = true;
break;
}
}
}


if(!$found == true)
{
for($j=0; $j< count($restaurants); $j++)
{
.....


}

下面是基于@Victory 的回答,添加了一个 elseif 语句,如果 while 循环通过了所需的订单号(假设这些现在是排序数组),则停止 while 循环。我认为这应该会提高效率(至少对于大阵列而言),但如果我错了请纠正我?

function orderArrayByOrder($a,$b)
{
return ($a->order < $b->order) ? -1 : 1;
}

$a1 = usort($people, "orderArrayByOrder");
$a2 = usort($sandwiches, "orderArrayByOrder");
$a3 = usort($restaurants, "orderArrayByOrder");


$c1 = count($a1)
$c2 = count($c2)
$c3 = count($c3)

$i1 = 0
$i2 = 0
$i3 = 0


// itertor over order
for ($curOrder ... $maxorder)
{

while ($i1 < $c1)
{
if($a1[$i1]->order == $curOrder)
{
//Do what I need to do
break;
}
elseif($a1[$i1]->order > $curOrder)
{
//We know the order won't exist in this array.
break;
}
$i1++;
}

while ($i2 < $c2)
{
if($a2[$i2]->order == $curOrder)
{
//Do what I need to do
break;
}
elseif($a2[$i2]->order > $curOrder)
{
break;
}
$i1++;
}

}

最佳答案

基本上您需要对每个数组进行排序并找到最大顺序,然后遍历顺序索引并打印具有给定顺序的项目。这是 O(N Log(N)) 因为排序时 N = max number of elements

伪代码

  • 对每个数组进行排序(在 php 中使用 usort)- O(N log(N))

  • 找到 maxorder(遍历每个)- O(N)

  • 为每个索引创建一个数组

  • 获取每个索引的长度并存储

$a1 = usort($people, function(){})
$a2 = usort($places, function(){})
$a3 = usort($things, function(){})

$c1 = count($a1)
$c2 = count($c2)
$c3 = count($c3)

$i1 = 0
$i2 = 0
$i3 = 0

// itertor over order
for ($curOrder ... $maxorder) {
// while $a1 is on current order and its index is in bound
while ($i1 < $c1 && $a1[$i1]->order == $curOrder) {
echo $a1[$i1]->value;
$i1++;
}

while ($i2 < $c2 && $a2->order == $curOrder) {
echo $a2[$i2]->value;
$i2++;
}
while ($i3 < $c3 && $a3->order == $curOrder) {
echo $a3[$i3]->value;
$i3++;
}
}

关于php - 使用多个嵌套循环的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32364735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com