Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 两个数组如何将每个内部对象键的边距进行交叉匹配?_Php_Laravel 5.8 - Fatal编程技术网

Php 两个数组如何将每个内部对象键的边距进行交叉匹配?

Php 两个数组如何将每个内部对象键的边距进行交叉匹配?,php,laravel-5.8,Php,Laravel 5.8,我有两个阵列用户和游戏 $user =[ { "name": "jone", "id": "100" }, { "name": "Peters", "id": "200" } ] $game = [ { "name": "tennis", "level": "05",

我有两个阵列用户和游戏

$user =[
       {
           "name": "jone",
           "id": "100"
       },
       {
           "name": "Peters",
           "id": "200"
       }
   ]

$game = [
       {
           "name": "tennis",
           "level": "05",
           "user_id": "100"
       },
       {
           "name": "football",
           "level": "03",
           "user_id": "100"
       },
       {
           "name": "football",
           "level": "05",
           "user_id": "200"
       }
   ]
我想使用PHP/Laravel得到这样的结果

$user = [
          {
              "name": "jone",
              "id": "100"
              "game": [
                  {
                      "name": "tennis",
                      "level": "05",
                      "user_id": "100"
                  },
                  {
                      "name": "football",
                      "level": "03",
                      "user_id": "100"
                  }
              ],
          },
          {
              "name": "Peters",
              "id": "200"
              "game": [
                  {
                      "name": "football",
                      "level": "05",
                      "user_id": "200"
                  }
              ],
          }
      ],   

任何人帮助我

我明白我不应该回答这个非问题。我仍然这样做,因为我认为这可能会带来一些学习价值

我们的想法是,不要像天真的方法那样为每个用户循环每个游戏,因为这根本无法扩展。最好使用匹配的数组,然后将游戏排序到其中:

//Prepare matching array
$user_games=array();
foreach ($user as $u) {
        $u['game']=array();
        $user_games[$u['id']]=$u;
}

//Sort games into matching array
foreach ($game as $g) {
        $user_games[$g['user_id']]['game'][]=$g;
}
这样,一个新游戏将不会创建n个循环,n是用户数,而只是一个

print_r($user_games);
创建所需的输出。如果用户ID作为索引有问题,只需使用

print_r(array_values($user_games);

这不是一个代码编写服务,你应该至少向我们展示一些你自己尝试并解决这个问题的尝试。我是PHP新手帮助我请阅读指南,它会向你解释你之前应该尝试,做一些研究,给出一个不起作用的可复制示例。