Ask for the same value extraction and classification in the multi-dimensional array in PHP

example PHP has such a multi-dimensional array

Array[4][
  {
    "attr_id": "11",
    "attr_price": "29.8",
    "attr_value": "",
    "goods_attr_id": "31",
    "goods_id": "10"
  },{
    "attr_id": "8",
    "attr_price": "29.8",
    "attr_value": "60CM",
    "goods_attr_id": "29",
    "goods_id": "10"
  },{
    "attr_id": "8",
    "attr_price": "29.8",
    "attr_value": "70CM",
    "goods_attr_id": "30",
    "goods_id": "10"
  },{
    "attr_id": "11",
    "attr_price": "29.8",
    "attr_value": "",
    "goods_attr_id": "28",
    "goods_id": "10"
  }
]

ask for advice on how to extract the same "attr_id" into a new array such as:

$arr = new arrry();
$arr["a"] = [
  {
    "attr_id": "8",
    "attr_price": "29.8",
    "attr_value": "70CM",
    "goods_attr_id": "30",
    "goods_id": "10"
  },{
    "attr_id": "8",
    "attr_price": "29.8",
    "attr_value": "70CM",
    "goods_attr_id": "30",
    "goods_id": "10"
  },
]

$arr["b"] = [
   {
    "attr_id": "11",
    "attr_price": "29.8",
    "attr_value": "",
    "goods_attr_id": "31",
    "goods_id": "10"
    },{
    "attr_id": "11",
    "attr_price": "29.8",
    "attr_value": "",
    "goods_attr_id": "28",
    "goods_id": "10"
  }
]

the length of the array is uncertain, the attr_ id value and the number are uncertain, which is to judge that the same attr_id in the array will be put into the new array.

Php
Mar.13,2021

$arr = [
    [
        "attr_id": "11",
        "attr_price": "29.8",
        "attr_value": "",
        "goods_attr_id": "31",
        "goods_id": "10"
    ],
    [
        "attr_id": "8",
        "attr_price": "29.8",
        "attr_value": "60CM",
        "goods_attr_id": "29",
        "goods_id": "10"
    ],
    [
        "attr_id": "8",
        "attr_price": "29.8",
        "attr_value": "70CM",
        "goods_attr_id": "30",
        "goods_id": "10"
    ],
    [
        "attr_id": "11",
        "attr_price": "29.8",
        "attr_value": "",
        "goods_attr_id": "28",
        "goods_id": "10"
    ]
]

$arr1 = [];
foreach ($arr as $k => $v) {
    $arr1[$v[attr_id]][] = $v;
}

// 
$arr1 = [
    '8' => [
        [
            "attr_id": "8",
            "attr_price": "29.8",
            "attr_value": "70CM",
            "goods_attr_id": "30",
            "goods_id": "10"
        ],
        [
            "attr_id": "8",
            "attr_price": "29.8",
            "attr_value": "60CM",
            "goods_attr_id": "30",
            "goods_id": "10"
        ],
    ],
    '11' => [
        [
            "attr_id": "11",
            "attr_price": "29.8",
            "attr_value": "",
            "goods_attr_id": "31",
            "goods_id": "10"
        ],
        [
            "attr_id": "11",
            "attr_price": "29.8",
            "attr_value": "",
            "goods_attr_id": "28",
            "goods_id": "10"
        ],
    ],
]
Menu