我的数组包含以下内容:
Array ( [0] => Events - Central [1] => Central [2] => Finance [3] => 17 [5] => 11 [8] => 11 )
Array ( [0] => Events - Central [1] => Central [2] => HR [3] => 17 [5] => 11 [8] => 11 )
Array ( [0] => Events - Central [1] => Central [2] => IT [3] => 17 [5] => 11 [8] => 11 )
Array ( [0] => Events - Central [1] => London [2] => Sales [3] => 17 [5] => 11 [8] => 11 )
Array ( [0] => Events - Central [1] => London [2] => Marketing [3] => 17 [5] => 11 [8] => 11 )
Array ( [0] => Events - Central [1] => London [2] => HR [3] => 17 [5] => 11 [8] => 11 )
Array ( [0] => Piloting [1] => London [2] => HR [3] => 17 [5] => 11 [8] => 11 )
Array ( [0] => Piloting [1] => London [2] => IT [3] => 17 [5] => 11 [8] => 11 )
现在,使用一个简单的WHILE
循环允许我循环遍历数组,我可以轻松地提取我需要的信息,但我很难挣扎,因为我需要带回不同的值(我不能改变SQL
代码。)
输出将是这样的简单表格,但请注意表格的第1级和第2级不会重复:
Events - Central 17
Central 17
Finance 11
HR 1
IT 1
London 28
Sales 17
Marketing 2
HR 5
Piloting 37
London 37
HR 30
IT 7
简而言之,我试图恢复distinct
个实例 - 如果这有意义的话?
如果不解构阵列,这是否可行?
我很有意思,我可能没有很好地说明/构思这个问题。
答案 0 :(得分:1)
以下是您可以做的事情(a)回显您需要的数据,以及(b)以您可以使用的方式模糊地安排所述数据的数组:
<?php
$superArray = [
[ "Events - Central", "Central", "Finance", 17, 0, 11, 0, 0, 11, 0 ],
[ "Events - Central", "Central", "HR", 17, 0, 11, 0, 0, 11, 0 ],
[ "Events - Central", "Central", "IT", 17, 0, 11, 0, 0, 11, 0 ],
[ "Events - Central", "London", "Sales", 17, 0, 11, 0, 0, 11, 0 ],
[ "Events - Central", "London", "Marketing", 17, 0, 11, 0, 0, 11, 0 ],
[ "Events - Central", "London", "HR", 17, 0, 11, 0, 0, 11, 0 ],
[ "Piloting", "London", "HR", 17, 0, 11, 0, 0, 11, 0 ],
[ "Piloting", "London", "IT", 17, 0, 11, 0, 0, 11, 0 ]
];
usort($superArray, function ($a,$b) {
return $a[0] == $b[0] && $a[1]==$b[1]
? $a[2]<=>$b[2]
: (
$a[0] == $b[0]
?$a[1]<=>$b[1]
:$a[0]<=>$b[0]
);
});
$lastSeen = [];
$buildArray = [];
foreach ($superArray as $array) {
if (!isset($lastSeen[0]) || $lastSeen[0] != $array[0]) {
$lastSeen[0] = $array[0];
$lastSeen[1] = null;
echo $array[0]."\t".$array[3].PHP_EOL;
$buildArray[]= [
"level" => 0,
"name" => $array[0],
"value" => $array[3]
];
}
if (!isset($lastSeen[1]) || $lastSeen[1] != $array[1]) {
$lastSeen[1] = $array[1];
$lastSeen[2] = null;
echo "\t".$array[1]."\t".$array[5].PHP_EOL;
$buildArray[]= [
"level" => 1,
"name" => $array[1],
"value" => $array[5]
];
}
if (!isset($lastSeen[2]) || $lastSeen[2] != $array[2]) {
$lastSeen[2] = $array[2];
echo "\t\t".$array[2]."\t".$array[8].PHP_EOL;
$buildArray[]= [
"level" => 2,
"name" => $array[2],
"value" => $array[8]
];
}
}
print_r($buildArray);
这将打印:
Events - Central 17
Central 11
Finance 11
HR 11
IT 11
London 11
HR 11
Marketing 11
Sales 11
Piloting 17
London 11
HR 11
IT 11
数组结构将是:
Array
(
[0] => Array
(
[level] => 0
[name] => Events - Central
[value] => 17
)
[1] => Array
(
[level] => 1
[name] => Central
[value] => 11
)
[2] => Array
(
[level] => 2
[name] => Finance
[value] => 11
)
[3] => Array
(
[level] => 2
[name] => HR
[value] => 11
)
[4] => Array
(
[level] => 2
[name] => IT
[value] => 11
)
[5] => Array
(
[level] => 1
[name] => London
[value] => 11
)
[6] => Array
(
[level] => 2
[name] => HR
[value] => 11
)
[7] => Array
(
[level] => 2
[name] => Marketing
[value] => 11
)
[8] => Array
(
[level] => 2
[name] => Sales
[value] => 11
)
[9] => Array
(
[level] => 0
[name] => Piloting
[value] => 17
)
[10] => Array
(
[level] => 1
[name] => London
[value] => 11
)
[11] => Array
(
[level] => 2
[name] => HR
[value] => 11
)
[12] => Array
(
[level] => 2
[name] => IT
[value] => 11
)
)
这个想法是首先将所有数组放在一个组合数组中,然后根据优先级排序(我认为它们已经排序了)。完成后,您可以跟踪每个级别的内容,并根据该级别重新构建阵列。
请记住,这是非常硬编码的内容,但如果需要,可以概括一下。
示例:http://sandbox.onlinephpfunctions.com/code/bf92828a5561d59d503e5765cc6b566a5acc0532
答案 1 :(得分:1)
对apokryfos的回答略有不同,但是类似的概念。我们的想法是使用PHP中数组键的重复数据删除功能来构建分层数组:
$data = [];
$data[] = [0 => 'Events - Central', 1 => 'Central', 2 => 'Finance', 3 => 17, 5 => 11, 8 => 11];
$data[] = [0 => 'Events - Central', 1 => 'Central', 2 => 'HR', 3 => 17, 5 => 11, 8 => 11];
$data[] = [0 => 'Events - Central', 1 => 'Central', 2 => 'IT', 3 => 17, 5 => 11, 8 => 11];
$data[] = [0 => 'Events - Central', 1 => 'London', 2 => 'Sales', 3 => 17, 5 => 11, 8 => 11];
$data[] = [0 => 'Events - Central', 1 => 'London', 2 => 'Marketing', 3 => 17, 5 => 11, 8 => 11];
$data[] = [0 => 'Events - Central', 1 => 'London', 2 => 'HR', 3 => 17, 5 => 11, 8 => 11];
$data[] = [0 => 'Piloting', 1 => 'London', 2 => 'HR', 3 => 17, 5 => 11, 8 => 11];
$data[] = [0 => 'Piloting', 1 => 'London', 2 => 'IT', 3 => 17, 5 => 11, 8 => 11];
$tree_root = [];
// map the keys in the $data array to each other
$caption_to_value_mapping = [
0 => 3,
1 => 5,
2 => 8
];
foreach ($data as $datum) {
$sub_tree = &$tree_root;
foreach ($caption_to_value_mapping as $level_index => $value) {
$caption = $datum[$level_index];
if (!isset($sub_tree[$caption])) {
$sub_tree[$caption] = ['next_level' => []];
}
$sub_tree[$caption]['caption'] = $caption;
$sub_tree[$caption]['value'] = $datum[$value];
$sub_tree = &$sub_tree[$caption]['next_level'];
}
}
var_dump($tree_root);
然后,您可以在递归函数中使用$ tree_root来构建数据的显示。
请注意,这是通用的,只要维持$ caption_to_value_mapping,就会采用任意数量的级别。