我该如何排序这个数组

How can I sort this array

我得到了一个这样的数组。

array:3 [▼
  0 => array:2 [▼
    "id" => "1039"
    "total" => 2.3
  ]
  1 => array:2 [▼
    "id" => "1039"
    "total" => -3.0
  ]
  2 => array:2 [▼
    "id" => "10007"
    "total" => 15.5
  ]
]

我需要同一数组中相同“id”的“总计”。 例如对于“id”= 1039 数组应该是

[2.3, -3.0]

对于“id”= 10007 数组应该是

[15.5]

我想要的数组是

[
  [2.3, -3.0],
  [15.5]
]

您可以遍历数组并将其设置为另一个数组,如下所示:

$array = [
    ['id' => 1, 'value' => 2],
    ['id' => 1, 'value' => 3],
    ['id' => 2, 'value' => 5],
];

$temp = [];

foreach($array as $element){
    if(!isset($temp[$element['id']])){
        $temp[$element['id']] = [];
    }
    
    $temp[$element['id']][] = $element['value'];
}


var_dump($temp);

您真的希望它们由数组 ID 键入。

$arr = [ ["id" => "1039", "total" => 2.3],["id" => "1039","total" => -3.0],["id" => "10007","total" => 15.5]];

$total = [];
foreach($arr as $item) {
    $total[$item['id']][] = $item['total'];
}

那会 return

array:2 [
  1039 => array:2 [
    0 => 2.3
    1 => -3.0
  ]
  10007 => array:1 [
    0 => 15.5
  ]
]