在大型数组上应用时,PHP Foreach循环太慢 [英] PHP Foreach loop too slow when applied on large arrays

查看:231
本文介绍了在大型数组上应用时,PHP Foreach循环太慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,基本上,我不得不循环考虑一个由25000个项目组成的数组,然后将每个项目与另一个数组的ID进行比较,如果该ID来自第一个数组和第二个匹配项,则创建另一个匹配项数组。看起来像这样。

So, basically, i have to loop thought an array of 25000 items, then compare each item with another array's ID and if the ID's from the first array and the second match then create another array of the matched items. That looks something like this.

foreach ($all_games as $game) {
  foreach ($user_games_ids as $user_game_id) {
     if ($user_game_id == $game["appid"]) {
         $game_details['title'][] = $game["title"];
         $game_details['price'][] = $game["price"];
         $game_details['image'][] = $game["image_url"];
         $game_details['appid'][] = $game["appid"];
     }
  }
}

我只测试了这个循环第一个数组($ all_games)有2500条记录,第二个数组($ user_games_ids)有约2000条记录,据我估计,执行该代码块大约需要 10秒,仅循环执行。那是正常的吗?那应该花那么长时间吗,还是我是从错误的角度来解决这个问题?有办法减少时间吗?因为当我将该代码应用于25000条记录时,时间将大大增加。

I tested this loop with only 2500 records from the first array ($all_games) and about 2000 records from the second array ($user_games_ids) and as far as i figured, it takes about 10 seconds for the execution of that chunk of code, only the loops execution. Is that normal? Should that take that long or I'm I approaching the issue from the wrong side? Is there a way to reduce that time? Because when i apply that code to 25000 records that time will significantly increase.

感谢任何帮助,
谢谢。

Any help is appreciated, Thanks.

编辑:因此,没有混淆,我无法使用数据库查询来提高性能,尽管我已将所有25000个游戏添加到数据库中,但我不能对用户游戏ID进行相同的操作。据我所知,我无法通过我访问的API吸引所有用户,即使有,也确实会有很多用户。当用户在表单中输入ID时,我会即时获得用户游戏ID,基于此,我使用file_get_contents获取这些ID,然后将其与存储所有游戏的数据库进行交叉引用。再说一次,这可能不是最好的方法,但是我现在只能想到。

So there is no confusion, I can't use the DB query to improve the performance, although, i have added all 25000 games to the database, i can't do the same for the user games ids. There is no way that i know to get all of the users through that API i'm accessing, and even there is, that would be really a lot of users. I get user games ids on the fly when a user enters it's ID in the form and based on that i use file_get_contents to obtain those ids and then cross reference them with the database that stores all games. Again, that might not be the best way, but only one i could think of at this point.

推荐答案

使用 array_column()通过 appid 索引 $ game 数组,则可以将其简化为一个循环,然后只需检查数据是否已设置...

If you re-index the $game array by appid using array_column(), then you can reduce it to one loop and just check if the data isset...

$game = array_column($game,null,"appid");
foreach ($user_games_ids as $user_game_id) {
    if (isset( $game[$user_game_id])) {
        $game_details['title'][] = $game[$user_game_id]["title"];
        $game_details['price'][] = $game[$user_game_id]["price"];
        $game_details['image'][] = $game[$user_game_id]["image_url"];
        $game_details['appid'][] = $game[$user_game_id]["appid"];
    }
}

这篇关于在大型数组上应用时,PHP Foreach循环太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆