我得到了选定公司层次结构组合的关联数组的数组(某些层次结构级别可能是
null
)。
例如,如果层次结构为
division
> department
> team
:
[
0 => ["division" => "division1"]
1 => ["division" => "division2"]
2 => ["division" => "division1", "department" => "department10"]
3 => ["division" => "division1", "department" => null, "team" => "team91"]
4 => ["division" => "division1", "department" => "department10", "team" => "team85"]
5 => ["division" => "division3", "department" => "department999"]
6 => ["division" => "division2", "department" => "department33"]
7 => ["division" => "division1", "department" => null, "team" => "team92"]
]
我需要将其减少到仅包含唯一层次结构组合的数组,尽可能的最低层次结构(这可能不是正确的措辞,所以如果您有更好的想法,请编辑这句话或让我知道将其更新为什么)
我所说的“唯一”是指层次结构的唯一组合数组,没有组合的子集
上述示例应导致:
[
0 => ["division" => "division1", "department" => null, "team" => "team91"]
1 => ["division" => "division1", "department" => null, "team" => "team92"]
2 => ["division" => "division1", "department" => "department10", "team" => "team85"]
3 => ["division" => "division2", "department" => "department33"]
4 => ["division" => "division3", "department" => "department999"]
]
<?php
$data = [
["division" => "division1"],
["division" => "division2"],
["division" => "division1", "department" => "department10"],
["division" => "division1", "department" => null, "team" => "team91"],
["division" => "division1", "department" => "department10", "team" => "team85"],
["division" => "division3", "department" => "department999"],
["division" => "division2", "department" => "department33"],
["division" => "division1", "department" => null, "team" => "team92"]
];
function getHierarchyKey($item) {
// Create a unique key for each hierarchy level by concatenating non-null values
return implode(':', array_filter($item, function($value) { return !is_null($value); }));
}
// Step 1: Sort data by hierarchy level, from the most specific to the least specific
usort($data, function ($a, $b) {
return count(array_filter($b, fn($v) => !is_null($v))) <=> count(array_filter($a, fn($v) => !is_null($v)));
});
// Step 2: Filter out duplicates based on the unique key
$uniqueItems = [];
foreach ($data as $item) {
$key = getHierarchyKey($item);
if (!isset($uniqueItems[$key])) {
$uniqueItems[$key] = $item; // Add the most specific hierarchy combination
}
}
// Step 3: Get the values of unique items
$result = array_values($uniqueItems);
// Display the result
print_r($result);
希望这能满足您的需求。
因此,首先对数据进行排序,以便首先出现更具体(较低级别)的层次结构,然后我们通过其唯一键存储每个项目,跳过键上的重复项。导致我认为你所追求的。
按降序大小对数组行进行排序,然后逐步将结果数组中的行与输入数组中的每一行进行比较,以查看是否有任何行被结果行的数据完全“覆盖”。 演示
array_multisort(array_map('count', $array), SORT_DESC, $array);
$result = [];
foreach ($array as $row) {
foreach ($result as $saved) {
if (!array_diff_assoc($row, $saved)) {
continue 2;
}
}
$result[] = $row;
}
var_export($result);
输出:
array (
0 =>
array (
'division' => 'division1',
'department' => NULL,
'team' => 'team91',
),
1 =>
array (
'division' => 'division1',
'department' => NULL,
'team' => 'team92',
),
2 =>
array (
'division' => 'division1',
'department' => 'department10',
'team' => 'team85',
),
3 =>
array (
'division' => 'division2',
'department' => 'department33',
),
4 =>
array (
'division' => 'division3',
'department' => 'department999',
),
)