array:3 [
0 => array:5 [
"id" => 1
"object_module" => "CALENDAR"
"object_id" => 7
"target_module" => "PROJECT"
"target_id" => 51
]
1 => array:5 [
"id" => 2
"object_module" => "CALENDAR"
"object_id" => 7
"target_module" => "PROJECT"
"target_id" => 52
]
2 => array:5 [
"id" => 3
"object_module" => "CALENDAR"
"object_id" => 7
"target_module" => "CUSTOMER"
"target_id" => 50
]
]
按照 target_module
进行分组:
<?php
$json = '[{"id":1,"object_module":"CALENDAR","object_id":7,"target_module":"PROJECT","target_id":51},{"id":2,"object_module":"CALENDAR","object_id":7,"target_module":"PROJECT","target_id":52},{"id":3,"object_module":"CALENDAR","object_id":7,"target_module":"CUSTOMER","target_id":50}]';
$array = json_decode($json, true);
$keys = array_unique(array_column($array, 'target_module'));
$array2 = [];
foreach ($keys as $key) {
$array2[$key] = array_filter($array, function ($value) use ($key) {
return $value['target_module'] === $key;
});
}
分组结果:
array:2 [
"PROJECT" => array:2 [
0 => array:5 [
"id" => 1
"object_module" => "CALENDAR"
"object_id" => 7
"target_module" => "PROJECT"
"target_id" => 51
]
1 => array:5 [
"id" => 2
"object_module" => "CALENDAR"
"object_id" => 7
"target_module" => "PROJECT"
"target_id" => 52
]
]
"CUSTOMER" => array:1 [
2 => array:5 [
"id" => 3
"object_module" => "CALENDAR"
"object_id" => 7
"target_module" => "CUSTOMER"
"target_id" => 50
]
]
]