Php 如何推送具有相同值的数组

Php 如何推送具有相同值的数组,php,arrays,Php,Arrays,我有这样一个数组: "permissions" => array:4 [▼ 0 => array:8 [▼ "id" => 1 "name" => "UserIndex" "desc" => "查看所有用户" "category_id" => 1 "created_at" => null "updated_at" => null

我有这样一个数组:

"permissions" => array:4 [▼
      0 => array:8 [▼
        "id" => 1
        "name" => "UserIndex"
        "desc" => "查看所有用户"
        "category_id" => 1
        "created_at" => null
        "updated_at" => null
        "pivot" => array:2 [▶]
        "category" => array:2 [▼
          "id" => 1
          "name" => "用户"
        ]
      ]
      1 => array:8 [▼
        "id" => 2
        "name" => "UserDelete"
        "desc" => "删除用户"
        "category_id" => 1
        "created_at" => null
        "updated_at" => null
        "pivot" => array:2 [▶]
        "category" => array:2 [▼
          "id" => 1
          "name" => "用户"
        ]
      ]
      2 => array:8 [▼
        "id" => 3
        "name" => "UserInfo"
        "desc" => "查看用户"
        "category_id" => 1
        "created_at" => null
        "updated_at" => null
        "pivot" => array:2 [▶]
        "category" => array:2 [▼
          "id" => 1
          "name" => "用户"
        ]
      ]
      3 => array:8 [▼
        "id" => 4
        "name" => "ActivityIndex"
        "desc" => "查看所有活动"
        "category_id" => 2
        "created_at" => null
        "updated_at" => null
        "pivot" => array:2 [▶]
        "category" => array:2 [▼
          "id" => 2
          "name" => "活动"
        ]
      ]
"permission" => array:2[
 "用户"=> array:3[
   [
     "id" => 1
     .....
   ],
   [
     "id"=> 2
   ],
   [
    "id"=>3
   ]
  ],
  "活动"=> array:1[
   [
    "id" => 4
   ]
   ]
]
如您所见,该数组有一个名为“permissions”的键,它还有四个子数组(可能更多)。在这些子数组中,它们具有“category_id”键,该键链接到“category”键的“name”。我想做的是将相同的“category_id”键数组推送到一个新数组中。输出应如下所示:

"permissions" => array:4 [▼
      0 => array:8 [▼
        "id" => 1
        "name" => "UserIndex"
        "desc" => "查看所有用户"
        "category_id" => 1
        "created_at" => null
        "updated_at" => null
        "pivot" => array:2 [▶]
        "category" => array:2 [▼
          "id" => 1
          "name" => "用户"
        ]
      ]
      1 => array:8 [▼
        "id" => 2
        "name" => "UserDelete"
        "desc" => "删除用户"
        "category_id" => 1
        "created_at" => null
        "updated_at" => null
        "pivot" => array:2 [▶]
        "category" => array:2 [▼
          "id" => 1
          "name" => "用户"
        ]
      ]
      2 => array:8 [▼
        "id" => 3
        "name" => "UserInfo"
        "desc" => "查看用户"
        "category_id" => 1
        "created_at" => null
        "updated_at" => null
        "pivot" => array:2 [▶]
        "category" => array:2 [▼
          "id" => 1
          "name" => "用户"
        ]
      ]
      3 => array:8 [▼
        "id" => 4
        "name" => "ActivityIndex"
        "desc" => "查看所有活动"
        "category_id" => 2
        "created_at" => null
        "updated_at" => null
        "pivot" => array:2 [▶]
        "category" => array:2 [▼
          "id" => 2
          "name" => "活动"
        ]
      ]
"permission" => array:2[
 "用户"=> array:3[
   [
     "id" => 1
     .....
   ],
   [
     "id"=> 2
   ],
   [
    "id"=>3
   ]
  ],
  "活动"=> array:1[
   [
    "id" => 4
   ]
   ]
]

听起来很直截了当,您在输入数组上实现了一个循环,并逐个填充一个新的输出数组:

<?php
$input = [
  "permissions" => [
    [
      "id" => 1,
      "name" => "UserIndex",
      "desc" => "查看所有用户",
      "category_id" => 1,
      "created_at" => null,
      "updated_at" => null,
      "pivot" => [],
      "category" => [
        "id" => 1,
        "name" => "用户"
      ]
    ],
    [
      "id" => 2,
      "name" => "UserDelete",
      "desc" => "删除用户",
      "category_id" => 1,
      "created_at" => null,
      "updated_at" => null,
      "pivot" => [],
      "category" => [
        "id" => 1,
        "name" => "用户"
      ]
    ],
    [
      "id" => 3,
      "name" => "UserInfo",
      "desc" => "查看用户",
      "category_id" => 1,
      "created_at" => null,
      "updated_at" => null,
      "pivot" => [],
      "category" => [
        "id" => 1,
        "name" => "用户"
      ]
    ],
    [
      "id" => 4,
      "name" => "ActivityIndex",
      "desc" => "查看所有活动",
      "category_id" => 2,
      "created_at" => null,
      "updated_at" => null,
      "pivot" => [],
      "category" => [
        "id" => 2,
        "name" => "活动"
      ]
    ]
  ]
];

$output = [];

array_walk($input["permissions"], function($entry) use (&$output){
    $output[$entry["category"]["name"]][] = $entry;
});

print_r($output);

那么,实际问题是什么?您描述了期望的结果是什么。现在,在输入数组上实现一个循环,并逐个填充一个新的输出数组。Thx,这就是我需要的。起初,我在考虑如何在“foreach”方法中控制数组索引,因为我想逐个比较它的“category_id”,但不幸的是,我无法使循环正确地迭代。我还没有听说过“array\u walk”方法,我认为是时候深入研究它了。你可以用
foreach
循环做同样的事情。我个人更喜欢lamda表示法,因为临时迭代变量不会在循环之外的范围内乱七八糟,所以它通常会使事情更干净。但这是个人偏好。