在 foreach 循环中调用方法和嵌套循环是否明智?

发布于 2024-09-24 07:03:23 字数 350 浏览 1 评论 0原文

您好,我有一个返回支持票证数组的方法。每个支持票证可以有许多注释,因此我有一个方法可以返回具有该票证 ID 的票证注释数组。我想在票证旁边显示注释,这意味着将获取注释嵌套在 foreach 循环内。

foreach($tickets as $ticket){
     //display ticket info

     //now get ticket notes using method getNotes()

     foreach($ticketnote as $note){
         //display note
     }
}   

像这样的嵌套循环会对性能产生影响吗?这是好的做法吗?

Hi I have a method that returns an array of support tickets. Each support ticket can have many notes so I have a method that returns an array of tickets notes with that ticket id. I want to display the notes alongside the ticket which would mean nesting the get notes inside the foreach loop.

foreach($tickets as $ticket){
     //display ticket info

     //now get ticket notes using method getNotes()

     foreach($ticketnote as $note){
         //display note
     }
}   

Do nested loops like this have performance implications? Is this good practice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

情话已封尘 2024-10-01 07:03:23

这不是问题。

像这样的嵌套循环会对性能产生影响吗?这是好的做法吗?

嵌套循环没有特定的性能影响。

当然,可能会处理大量数据;根据它的大小,您可能会达到内存或性能限制。但这是给定的,如果您使用不同的控制结构而不是嵌套循环,也会发生这种情况。

基于数组/foreach() 的解决方案始终需要在开始处理之前将完整的数据集加载到内存中。

如果您要从数据库中获取数据,则可以考虑重新构造函数,以便它们逐条获取和处理数据库记录,而不是将它们全部加载到数组中,然后对它们进行 foreach 处理。这允许您处理大于脚本内存限制的数据集。

It's not a problem.

Do nested loops like this have performance implications? Is this good practice?

Nested loops have no specific performance implications.

But of course, a lot of data may be processed; depending on how much it is, you could reach memory or performance limits. But that is a given, and would also occur if you would use a different control structure instead of a nested loop.

An array-/foreach()-based solution will always require loading the full data set into memory before it starts processing.

If you are fetching data from a database, you could consider re-structuring your functions so they fetch and process a database record one by one instead of loading them all into an array, and foreaching over them. That allows you to process data sets that are larger than your script's memory limit.

失而复得 2024-10-01 07:03:23

嗯,程序会按照你的指示去做。它将遍历所有票据的每张票据。如果必须这样做——那就必须这样做。到目前为止,如果您必须循环遍历所有这些,则不存在更好的实践。唯一的性能影响是比没有嵌套循环时需要更多的迭代,但是如果没有嵌套循环,您将得不到任何结果。

Well, the program does what you tell it to do. It will pass through each note of all the tickets. If that has to be done - it has to be done. So far a better practice doesn't exist if you have to loop through all of them. The only performance implication is more iterations than it would be without the nested loop, but without the nested loop you'd get no results.

拥醉 2024-10-01 07:03:23

如果您有过多的票证和票据,只会对性能产生影响。因此,如果您有 1000 张票,每张票有 1000 张票据,则内循环将运行 c。 1,000,000 次。但是,正如其他人所说,如果有必要这样做,那就是必要的。

There are only performance implications if you have excessive numbers of tickets and notes. So, if you had 1000 tickets and each had 1000 notes, the inner loop would run c. 1,000,000 times. But, as others have said, if it's necessary to do it this way then it's necessary.

迷爱 2024-10-01 07:03:23

其他人已经为您指明了正确的方向。

然而,另一种尚未提及且值得(可能?)研究的方法是 Spl 迭代器

$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));

foreach($iter as $key=>$value) {
    echo $key.' =>'.$value;
}

Others have already pointed you in the right direction.

However, another aproach which has yet to be mentioned, and worth(possibly?) looking into are the Spl Iterators

$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));

foreach($iter as $key=>$value) {
    echo $key.' =>'.$value;
}
只是我以为 2024-10-01 07:03:23

第一次观察时,由于无论如何您都会显示“票据注释”,因此无论您在单个循环还是两个嵌套循环中执行此操作都没有多大关系。净迭代计数将相同。

在呈现问题上,一次显示所有这些信息并不明智。您肯定会想要应用某种分页。

On first observation, since you would be displaying the 'ticket notes' anyway, it wouldn't matter much whether you do this in a single loop or two nested loops. The net iteration count would be same.

On presentation issue, it's not wise to display all this information at once. You would certainly want to apply some kind of pagination.

你是我的挚爱i 2024-10-01 07:03:23

绝对不是一个好的做法,但我从来没有找到比这更好的解决方案。

Definitely not a good practice but i've never found a better solution than this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文