php – 使用多个嵌套循环的最佳方法

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 使用多个嵌套循环的最佳方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用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++)
   {
   .....


}

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

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循环(假设这些是现在已排序的数组).我相信这应该提高效率(至少对于大阵列),但如果我错了请纠正我?

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++;
   }

}

解决方法

基本上你需要对每个数组进行排序并找到maxorder,然后循环遍历订单索引并使用给定的顺序打印项目.这是O(N LOG(N)),因为排序其中N =最大元素数

这是一些伪代码

>对每个数组进行排序(在PHP中使用usort) – O(N log(N))
> find 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 &amp;& $a2->order == $curOrder) {
      echo $a2[$i2]->value;
      $i2++;
  }
   while ($i3 < $c3 && $a3->order == $curOrder) {
      echo $a3[$i3]->value;
      $i3++;
  }
}

脚本宝典总结

以上是脚本宝典为你收集整理的php – 使用多个嵌套循环的最佳方法全部内容,希望文章能够帮你解决php – 使用多个嵌套循环的最佳方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。