英文:
Convert 2d array with 3 columns into a hierarchical structure with no associative keys
问题
以下是您提供的内容的翻译:
我有这样的数据:
这也可以看作是这个PHP数组:
$items = [
['item1' => 'a', 'item2' => 'c', 'item3' => 'h'],
['item1' => 'a', 'item2' => 'c', 'item3' => 'i'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'j'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'k'],
['item1' => 'b', 'item2' => 'e', 'item3' => 'l'],
['item1' => 'b', 'item2' => 'e', 'item3' => 'm'],
['item1' => 'b', 'item2' => 'f', 'item3' => 'n'],
['item1' => 'b', 'item2' => 'g', 'item3' => 'o'],
];
或者是这个(多维)列表:
a
c
h
i
d
j
k
b
e
l
m
f
n
g
o
我应该如何循环遍历这些数据并创建一个多维数组,如下所示:
$items = [
[a, [[c, [h, i]], [d, [j, k]]],
[b, [[e, [l, m]], [f, [n]], [g, [o]]]
];
基本上,我的目标是构建多维HTML列表,但我被困住了。
有了多维数组,我就能做到了。
谢谢!
这是尝试解决它的一种方法。
这是一个稍微复杂的例子。
它有一些索引错误。
我想知道是否有更清晰的方法。
<?php
$items = [
['item1' => 'a', 'item2' => 'c', 'item3' => 'd2', 'item4' => 'f'],
['item1' => 'a', 'item2' => 'c', 'item3' => 'd2', 'item4' => 'g'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'd3', 'item4' => 'h'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'e', 'item4' => 'i'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'j'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'k'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'l'],
['item1' => 'b', 'item2' => 'c2', 'item3' => 'e', 'item4' => 'm'],
];
$items_new = [];
$item1 = '';
$item2 = '';
$item3 = '';
$item4 = '';
foreach ($items as $item):
$item1_new = $item['item1'];
$item2_new = $item['item2'];
$item3_new = $item['item3'];
$item4_new = $item['item4'];
if($item1 != $item1_new)
{
$items_new[] = [$item1_new, []];
$item1 = $item1_new;
$item2 = '';
}
if($item2 != $item2_new)
{
$items_new[count($items_new) - 1][] = [$item2_new, []];
$item2 = $item2_new;
$item3 = '';
}
if($item3 != $item3_new)
{
$items_new[count($items_new) - 1][count($items_new[1]) - 1][] = [$item3_new, []];
$item3 = $item3_new;
$item4 = '';
}
if($item4 != $item4_new)
{
$items_new[count($items_new) - 1][count($items_new[1]) - 1][count($items_new[2]) - 1][] = [$item4_new, []];
$item4 = $item4_new;
}
endforeach;
foreach($items_new as $items1)
{
if (is_string($items1))
{
echo $items1 . '<br>';
}
elseif (is_array($items1))
{
foreach($items1 as $items2)
{
if (is_string($items2))
{
echo ' - ' . $items2 . '<br>';
}
elseif (is_array($items2))
{
foreach ($items2 as $items3)
{
if (is_string($items3))
{
echo ' --- ' . $items3 . '<br>';
}
elseif (is_array($items3))
{
foreach ($items3 as $items4)
{
if (is_string($items4))
{
echo ' ----- ' . $items4 . '<br>';
}
elseif (is_array($items4))
{
foreach ($items4 as $items5)
{
if (is_string($items5))
{
echo ' ------- ' . $items5 . '<br>';
}
elseif (is_array($items5))
{
foreach ($items5 as $items6)
{
}
}
}
}
}
}
}
}
}
}
}
?>
英文:
I have data like this:
Which can also be seen as this PHP array:
$items = [
['item1' => 'a', 'item2' => 'c', 'item3' => 'h'],
['item1' => 'a', 'item2' => 'c', 'item3' => 'i'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'j'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'k'],
['item1' => 'b', 'item2' => 'e', 'item3' => 'l'],
['item1' => 'b', 'item2' => 'e', 'item3' => 'm'],
['item1' => 'b', 'item2' => 'f', 'item3' => 'n'],
['item1' => 'b', 'item2' => 'g', 'item3' => 'o'],
];
Or even this (multidimensional) list:
a
c
h
i
d
j
k
b
e
l
m
f
n
g
o
How could I loop through this data and create a multidimensional array like
$items = [
[a, [[c, [h, i]], [d, [j, k]]],
[b, [[e, [l, m]], [f, [n]], [g, [o]]]
];
Basically my aim to build the multidimensional html list, but my brain is stuck.
With the multidimensional array I'd be able to do it.
Thanks!
Here's an attempt to solve it.
It's a bit more complicated example.
And it's "too hard coded".
There are some errors in the indexes.
I'm wondering if there's a cleaner approach.
<?php
$items = [
['item1' => 'a', 'item2' => 'c', 'item3' => 'd2', 'item4' => 'f'],
['item1' => 'a', 'item2' => 'c', 'item3' => 'd2', 'item4' => 'g'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'd3', 'item4' => 'h'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'e', 'item4' => 'i'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'j'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'k'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'l'],
['item1' => 'b', 'item2' => 'c2', 'item3' => 'e', 'item4' => 'm'],
];
$items_new = [];
$item1 = '';
$item2 = '';
$item3 = '';
$item4 = '';
foreach ($items as $item):
$item1_new = $item['item1'];
$item2_new = $item['item2'];
$item3_new = $item['item3'];
$item4_new = $item['item4'];
if($item1 != $item1_new)
{
$items_new[] = [$item1_new, []];
$item1 = $item1_new;
$item2 = '';
}
if($item2 != $item2_new)
{
$items_new[count($items_new) - 1][] = [$item2_new, []];
$item2 = $item2_new;
$item3 = '';
}
if($item3 != $item3_new)
{
$items_new[count($items_new) - 1][count($items_new[1]) - 1][] = [$item3_new, []];
$item3 = $item3_new;
$item4 = '';
}
if($item4 != $item4_new)
{
$items_new[count($items_new) - 1][count($items_new[1]) - 1][count($items_new[2]) - 1][] = [$item4_new, []];
$item4 = $item4_new;
}
endforeach;
foreach($items_new as $items1)
{
if (is_string($items1))
{
echo $items1 . '<br>';
}
elseif (is_array($items1))
{
foreach($items1 as $items2)
{
if (is_string($items2))
{
echo ' - ' . $items2 . '<br>';
}
elseif (is_array($items2))
{
foreach ($items2 as $items3)
{
if (is_string($items3))
{
echo ' --- ' . $items3 . '<br>';
}
elseif (is_array($items3))
{
foreach ($items3 as $items4)
{
if (is_string($items4))
{
echo ' ----- ' . $items4 . '<br>';
}
elseif (is_array($items4))
{
foreach ($items4 as $items5)
{
if (is_string($items5))
{
echo ' ------- ' . $items5 . '<br>';
}
elseif (is_array($items5))
{
foreach ($items5 as $items6)
{
}
}
}
}
}
}
}
}
}
}
}
?>
答案1
得分: 1
使用引用来避免跟踪显式索引,当您推送和管理数据分组时。
一个引用将确定是否需要实例化新的顶级行并将其推送到结果数组中。另一个引用将确定是否需要实例化唯一的第二级行并将其推送到其父级。
代码: (演示)
$result = [];
foreach ($items as $row) {
$k1 = $row['item1'];
$k2 = $row['item1'] . '_' . $row['item2'];
if (!isset($ref2[$k2])) {
$ref2[$k2] = [$row['item2'], [$row['item3']]];
if (!isset($ref1[$k1])) {
$ref1[$k1] = [$row['item1'], &$ref2[$k2]];
$result[] = &$ref1[$k1];
} else {
$ref1[$k1][] = &$ref2[$k2];
}
} else {
$ref2[$k2][1][] = $row['item3'];
}
}
echo json_encode($result);
输出(带有一些换行和缩进以提高可读性):
[
["a", ["c", ["h", "i"]], ["d", ["j", "k"]]],
["b", ["e", ["l", "m"]], ["f", ["n"]], ["g", ["o"]]]
]
以上片段可以通过在一组相似的条件内再次包装该过程来扩展以容纳每行4个元素。请注意(与第一个片段一样),只有最低级别的元素没有声明为引用。演示
$items = [
['item1' => 'a', 'item2' => 'c', 'item3' => 'd2', 'item4' => 'f'],
['item1' => 'a', 'item2' => 'c', 'item3' => 'd2', 'item4' => 'g'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'd3', 'item4' => 'h'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'e', 'item4' => 'i'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'j'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'k']
];
$result = [];
foreach ($items as $row) {
$k1 = $row['item1'];
$k2 = $row['item1'] . '_' . $row['item2'];
$k3 = $row['item1'] . '_' . $row['item2'] . '_' . $row['item3'];
if (!isset($ref3[$k3])) {
$ref3[$k3] = [$row['item3'], [$row['item4']]];
if (!isset($ref2[$k2])) {
$ref2[$k2] = [$row['item2'], &$ref3[$k3]];
if (!isset($ref1[$k1])) {
$ref1[$k1] = [$row['item1'], &$ref2[$k2]];
$result[] = &$ref1[$k1];
} else {
$ref1[$k1][] = &$ref2[$k2];
}
} else {
$ref2[$k2][1][] = &$ref3[$k3];
}
} else {
$ref3[$k3][1][] = $row['item4'];
}
}
echo json_encode($result);
输出:
[
["a", ["c", ["d2", ["f", "g"]]], ["d", ["d3", ["h"], ["e", ["i"]]]]],
["b", ["c", ["e", ["j", "k"]]]]
]
英文:
Use references to avoid keeping track of explicit indexes as you push and manage data grouping.
One reference will determine if a new top level row needs to be instantiated and pushed into the result array. Another reference will determine if a unique second level row needs to be instantiated and pushed into its parent.
Code: (Demo)
$result = [];
foreach ($items as $row) {
$k1 = $row['item1'];
$k2 = $row['item1'] . '_' . $row['item2'];
if (!isset($ref2[$k2])) {
$ref2[$k2] = [$row['item2'], [$row['item3']]];
if (!isset($ref1[$k1])) {
$ref1[$k1] = [$row['item1'], &$ref2[$k2]];
$result[] = &$ref1[$k1];
} else {
$ref1[$k1][] = &$ref2[$k2];
}
} else {
$ref2[$k2][1][] = $row['item3'];
}
}
echo json_encode($result);
Output (with some newlines and tabbing to improve readability):
[
["a",["c",["h","i"]],["d",["j","k"]]],
["b",["e",["l","m"]],["f",["n"]],["g",["o"]]]
]
The above snippet can be extended to accommodate 4 elements per row by wrapping the process inside another set of similar conditions. Notice that (like the first snippet) only the lowest level element us not declared as a reference. Demo
$items = [
['item1' => 'a', 'item2' => 'c', 'item3' => 'd2', 'item4' => 'f'],
['item1' => 'a', 'item2' => 'c', 'item3' => 'd2', 'item4' => 'g'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'd3', 'item4' => 'h'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'e', 'item4' => 'i'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'j'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'k']
];
$result = [];
foreach ($items as $row) {
$k1 = $row['item1'];
$k2 = $row['item1'] . '_' . $row['item2'];
$k3 = $row['item1'] . '_' . $row['item2'] . '_' . $row['item3'];
if (!isset($ref3[$k3])) {
$ref3[$k3] = [$row['item3'], [$row['item4']]];
if (!isset($ref2[$k2])) {
$ref2[$k2] = [$row['item2'], &$ref3[$k3]];
if (!isset($ref1[$k1])) {
$ref1[$k1] = [$row['item1'], &$ref2[$k2]];
$result[] = &$ref1[$k1];
} else {
$ref1[$k1][] = &$ref2[$k2];
}
} else {
$ref2[$k2][1][] = &$ref3[$k3];
}
} else {
$ref3[$k3][1][] = $row['item4'];
}
}
echo json_encode($result);
Output:
[
["a",["c",["d2",["f","g"]]],["d",["d3",["h"],["e",["i"]]]]],
["b",["c",["e",["j","k"]]]]
]
答案2
得分: 0
已解决!
$items = [
['item1' => 'a', 'item2' => 'c', 'item3' => 'd2', 'item4' => 'f'],
['item1' => 'a', 'item2' => 'c', 'item3' => 'd1', 'item4' => 'g'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'd3', 'item4' => 'h'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'e', 'item4' => 'i'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'j'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'k'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e1', 'item4' => 'l'],
['item1' => 'b', 'item2' => 'c2', 'item3' => 'e', 'item4' => 'm'],
['item1' => 'x', 'item2' => 'c2', 'item3' => 'e', 'item4' => 'm'],
['item1' => 'x', 'item2' => 'c2', 'item3' => 'e2', 'item4' => 'n'],
];
$items_new = [];
$item1 = '';
$count1 = 0;
foreach ($items as $item):
$item1_new = $item['item1'];
$item2_new = $item['item2'];
$item3_new = $item['item3'];
$item4_new = $item['item4'];
if ($item1 != $item1_new) {
$items_new[] = [$item1_new, []];
$item1 = $item1_new;
$count1++;
$item2 = '';
$count2 = 0;
}
if ($item2 != $item2_new) {
$items_new[$count1 - 1][$count2 + 1] = [$item2_new, []];
$item2 = $item2_new;
$count2++;
$item3 = '';
$count3 = 0;
}
if ($item3 != $item3_new) {
$items_new[$count1 - 1][$count2 + 1 - 1][$count3 + 1] = [$item3_new, []];
$item3 = $item3_new;
$count3++;
$item4 = '';
$count4 = 0;
}
if ($item4 != $item4_new) {
$items_new[$count1 - 1][$count2 + 1 - 1][$count3 + 1 - 1][$count4 + 1] = [$item4_new];
$item4 = $item4_new;
$count4++;
}
endforeach;
foreach ($items_new as $items1) {
if (is_string($items1)) {
echo $items1 . '<br>';
} else {
foreach ($items1 as $items2) {
if (is_string($items2)) {
echo ' - ' . $items2 . '<br>';
} else {
foreach ($items2 as $items3) {
if (is_string($items3)) {
echo ' --- ' . $items3 . '<br>';
} else {
foreach ($items3 as $items4) {
if (is_string($items4)) {
echo ' ----- ' . $items4 . '<br>';
} else {
foreach ($items4 as $items5) {
if (is_string($items5)) {
echo ' ------- ' . $items5 . '<br>';
}
}
}
}
}
}
}
}
}
}
以上是您提供的 PHP 代码的翻译部分。
英文:
Solved!
$items = [
['item1' => 'a', 'item2' => 'c', 'item3' => 'd2', 'item4' => 'f'],
['item1' => 'a', 'item2' => 'c', 'item3' => 'd1', 'item4' => 'g'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'd3', 'item4' => 'h'],
['item1' => 'a', 'item2' => 'd', 'item3' => 'e', 'item4' => 'i'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'j'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'k'],
['item1' => 'b', 'item2' => 'c', 'item3' => 'e1', 'item4' => 'l'],
['item1' => 'b', 'item2' => 'c2', 'item3' => 'e', 'item4' => 'm'],
['item1' => 'x', 'item2' => 'c2', 'item3' => 'e', 'item4' => 'm'],
['item1' => 'x', 'item2' => 'c2', 'item3' => 'e2', 'item4' => 'n'],
];
$items_new = [];
$item1 = '';
$count1 = 0;
foreach ($items as $item):
$item1_new = $item['item1'];
$item2_new = $item['item2'];
$item3_new = $item['item3'];
$item4_new = $item['item4'];
if($item1 != $item1_new)
{
$items_new[] = [$item1_new, []];
$item1 = $item1_new;
$count1++;
$item2 = '';
$count2 = 0;
}
if($item2 != $item2_new)
{
$items_new[$count1 - 1][$count2+1] = [$item2_new, []] ;
$item2 = $item2_new;
$count2++;
$item3 = '';
$count3 = 0;
}
if($item3 != $item3_new)
{
$items_new[$count1 - 1][$count2+1-1][$count3+1] = [$item3_new, []] ;
$item3 = $item3_new;
$count3++;
$item4 = '';
$count4 = 0;
}
if($item4 != $item4_new)
{
$items_new[$count1 - 1][$count2+1-1][$count3+1-1][$count4+1] = [$item4_new] ;
$item4 = $item4_new;
$count4++;
}
endforeach;
foreach($items_new as $items1)
{
if (is_string($items1))
{
echo $items1 . '<br>';
}
else
{
foreach($items1 as $items2)
{
if (is_string($items2))
{
echo ' - ' . $items2 . '<br>';
}
else
{
foreach ($items2 as $items3)
{
if (is_string($items3))
{
echo ' --- ' . $items3 . '<br>';
}
else
{
foreach ($items3 as $items4)
{
if (is_string($items4))
{
echo ' ----- ' . $items4 . '<br>';
}
else
{
foreach ($items4 as $items5)
{
if (is_string($items5))
{
echo ' ------- ' . $items5 . '<br>';
}
}
}
}
}
}
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论