将具有3列的二维数组转换为没有关联键的分层结构。

huangapple go评论107阅读模式
英文:

Convert 2d array with 3 columns into a hierarchical structure with no associative keys

问题

以下是您提供的内容的翻译:

我有这样的数据:

将具有3列的二维数组转换为没有关联键的分层结构。

这也可以看作是这个PHP数组:

  1. $items = [
  2. ['item1' => 'a', 'item2' => 'c', 'item3' => 'h'],
  3. ['item1' => 'a', 'item2' => 'c', 'item3' => 'i'],
  4. ['item1' => 'a', 'item2' => 'd', 'item3' => 'j'],
  5. ['item1' => 'a', 'item2' => 'd', 'item3' => 'k'],
  6. ['item1' => 'b', 'item2' => 'e', 'item3' => 'l'],
  7. ['item1' => 'b', 'item2' => 'e', 'item3' => 'm'],
  8. ['item1' => 'b', 'item2' => 'f', 'item3' => 'n'],
  9. ['item1' => 'b', 'item2' => 'g', 'item3' => 'o'],
  10. ];

或者是这个(多维)列表:

  1. a
  2. c
  3. h
  4. i
  5. d
  6. j
  7. k
  8. b
  9. e
  10. l
  11. m
  12. f
  13. n
  14. g
  15. o

我应该如何循环遍历这些数据并创建一个多维数组,如下所示:

  1. $items = [
  2. [a, [[c, [h, i]], [d, [j, k]]],
  3. [b, [[e, [l, m]], [f, [n]], [g, [o]]]
  4. ];

基本上,我的目标是构建多维HTML列表,但我被困住了。
有了多维数组,我就能做到了。

谢谢!

这是尝试解决它的一种方法。
这是一个稍微复杂的例子。
它有一些索引错误。

我想知道是否有更清晰的方法。

  1. <?php
  2. $items = [
  3. ['item1' => 'a', 'item2' => 'c', 'item3' => 'd2', 'item4' => 'f'],
  4. ['item1' => 'a', 'item2' => 'c', 'item3' => 'd2', 'item4' => 'g'],
  5. ['item1' => 'a', 'item2' => 'd', 'item3' => 'd3', 'item4' => 'h'],
  6. ['item1' => 'a', 'item2' => 'd', 'item3' => 'e', 'item4' => 'i'],
  7. ['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'j'],
  8. ['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'k'],
  9. ['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'l'],
  10. ['item1' => 'b', 'item2' => 'c2', 'item3' => 'e', 'item4' => 'm'],
  11. ];
  12. $items_new = [];
  13. $item1 = '';
  14. $item2 = '';
  15. $item3 = '';
  16. $item4 = '';
  17. foreach ($items as $item):
  18. $item1_new = $item['item1'];
  19. $item2_new = $item['item2'];
  20. $item3_new = $item['item3'];
  21. $item4_new = $item['item4'];
  22. if($item1 != $item1_new)
  23. {
  24. $items_new[] = [$item1_new, []];
  25. $item1 = $item1_new;
  26. $item2 = '';
  27. }
  28. if($item2 != $item2_new)
  29. {
  30. $items_new[count($items_new) - 1][] = [$item2_new, []];
  31. $item2 = $item2_new;
  32. $item3 = '';
  33. }
  34. if($item3 != $item3_new)
  35. {
  36. $items_new[count($items_new) - 1][count($items_new[1]) - 1][] = [$item3_new, []];
  37. $item3 = $item3_new;
  38. $item4 = '';
  39. }
  40. if($item4 != $item4_new)
  41. {
  42. $items_new[count($items_new) - 1][count($items_new[1]) - 1][count($items_new[2]) - 1][] = [$item4_new, []];
  43. $item4 = $item4_new;
  44. }
  45. endforeach;
  46. foreach($items_new as $items1)
  47. {
  48. if (is_string($items1))
  49. {
  50. echo $items1 . '<br>';
  51. }
  52. elseif (is_array($items1))
  53. {
  54. foreach($items1 as $items2)
  55. {
  56. if (is_string($items2))
  57. {
  58. echo ' - ' . $items2 . '<br>';
  59. }
  60. elseif (is_array($items2))
  61. {
  62. foreach ($items2 as $items3)
  63. {
  64. if (is_string($items3))
  65. {
  66. echo ' --- ' . $items3 . '<br>';
  67. }
  68. elseif (is_array($items3))
  69. {
  70. foreach ($items3 as $items4)
  71. {
  72. if (is_string($items4))
  73. {
  74. echo ' ----- ' . $items4 . '<br>';
  75. }
  76. elseif (is_array($items4))
  77. {
  78. foreach ($items4 as $items5)
  79. {
  80. if (is_string($items5))
  81. {
  82. echo ' ------- ' . $items5 . '<br>';
  83. }
  84. elseif (is_array($items5))
  85. {
  86. foreach ($items5 as $items6)
  87. {
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. ?>
英文:

I have data like this:

将具有3列的二维数组转换为没有关联键的分层结构。

Which can also be seen as this PHP array:

  1. $items = [
  2. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;h&#39;],
  3. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;i&#39;],
  4. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;d&#39;, &#39;item3&#39; =&gt; &#39;j&#39;],
  5. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;d&#39;, &#39;item3&#39; =&gt; &#39;k&#39;],
  6. [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;e&#39;, &#39;item3&#39; =&gt; &#39;l&#39;],
  7. [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;e&#39;, &#39;item3&#39; =&gt; &#39;m&#39;],
  8. [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;f&#39;, &#39;item3&#39; =&gt; &#39;n&#39;],
  9. [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;g&#39;, &#39;item3&#39; =&gt; &#39;o&#39;],
  10. ];

Or even this (multidimensional) list:

  1. a
  2. c
  3. h
  4. i
  5. d
  6. j
  7. k
  8. b
  9. e
  10. l
  11. m
  12. f
  13. n
  14. g
  15. o

How could I loop through this data and create a multidimensional array like

  1. $items = [
  2. [a, [[c, [h, i]], [d, [j, k]]],
  3. [b, [[e, [l, m]], [f, [n]], [g, [o]]]
  4. ];

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.

  1. &lt;?php
  2. $items = [
  3. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;d2&#39;, &#39;item4&#39; =&gt; &#39;f&#39;],
  4. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;d2&#39;, &#39;item4&#39; =&gt; &#39;g&#39;],
  5. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;d&#39;, &#39;item3&#39; =&gt; &#39;d3&#39;, &#39;item4&#39; =&gt; &#39;h&#39;],
  6. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;d&#39;, &#39;item3&#39; =&gt; &#39;e&#39;, &#39;item4&#39; =&gt; &#39;i&#39;],
  7. [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;e&#39;, &#39;item4&#39; =&gt; &#39;j&#39;],
  8. [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;e&#39;, &#39;item4&#39; =&gt; &#39;k&#39;],
  9. [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;e&#39;, &#39;item4&#39; =&gt; &#39;l&#39;],
  10. [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;c2&#39;, &#39;item3&#39; =&gt; &#39;e&#39;, &#39;item4&#39; =&gt; &#39;m&#39;],
  11. ];
  12. $items_new = [];
  13. $item1 = &#39;&#39;;
  14. $item2 = &#39;&#39;;
  15. $item3 = &#39;&#39;;
  16. $item4 = &#39;&#39;;
  17. foreach ($items as $item):
  18. $item1_new = $item[&#39;item1&#39;];
  19. $item2_new = $item[&#39;item2&#39;];
  20. $item3_new = $item[&#39;item3&#39;];
  21. $item4_new = $item[&#39;item4&#39;];
  22. if($item1 != $item1_new)
  23. {
  24. $items_new[] = [$item1_new, []];
  25. $item1 = $item1_new;
  26. $item2 = &#39;&#39;;
  27. }
  28. if($item2 != $item2_new)
  29. {
  30. $items_new[count($items_new) - 1][] = [$item2_new, []];
  31. $item2 = $item2_new;
  32. $item3 = &#39;&#39;;
  33. }
  34. if($item3 != $item3_new)
  35. {
  36. $items_new[count($items_new) - 1][count($items_new[1]) - 1][] = [$item3_new, []];
  37. $item3 = $item3_new;
  38. $item4 = &#39;&#39;;
  39. }
  40. if($item4 != $item4_new)
  41. {
  42. $items_new[count($items_new) - 1][count($items_new[1]) - 1][count($items_new[2]) - 1][] = [$item4_new, []];
  43. $item4 = $item4_new;
  44. }
  45. endforeach;
  46. foreach($items_new as $items1)
  47. {
  48. if (is_string($items1))
  49. {
  50. echo $items1 . &#39;&lt;br&gt;&#39;;
  51. }
  52. elseif (is_array($items1))
  53. {
  54. foreach($items1 as $items2)
  55. {
  56. if (is_string($items2))
  57. {
  58. echo &#39; - &#39; . $items2 . &#39;&lt;br&gt;&#39;;
  59. }
  60. elseif (is_array($items2))
  61. {
  62. foreach ($items2 as $items3)
  63. {
  64. if (is_string($items3))
  65. {
  66. echo &#39; --- &#39; . $items3 . &#39;&lt;br&gt;&#39;;
  67. }
  68. elseif (is_array($items3))
  69. {
  70. foreach ($items3 as $items4)
  71. {
  72. if (is_string($items4))
  73. {
  74. echo &#39; ----- &#39; . $items4 . &#39;&lt;br&gt;&#39;;
  75. }
  76. elseif (is_array($items4))
  77. {
  78. foreach ($items4 as $items5)
  79. {
  80. if (is_string($items5))
  81. {
  82. echo &#39; ------- &#39; . $items5 . &#39;&lt;br&gt;&#39;;
  83. }
  84. elseif (is_array($items5))
  85. {
  86. foreach ($items5 as $items6)
  87. {
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. ?&gt;

答案1

得分: 1

使用引用来避免跟踪显式索引,当您推送和管理数据分组时。

一个引用将确定是否需要实例化新的顶级行并将其推送到结果数组中。另一个引用将确定是否需要实例化唯一的第二级行并将其推送到其父级。

代码: (演示)

  1. $result = [];
  2. foreach ($items as $row) {
  3. $k1 = $row['item1'];
  4. $k2 = $row['item1'] . '_' . $row['item2'];
  5. if (!isset($ref2[$k2])) {
  6. $ref2[$k2] = [$row['item2'], [$row['item3']]];
  7. if (!isset($ref1[$k1])) {
  8. $ref1[$k1] = [$row['item1'], &$ref2[$k2]];
  9. $result[] = &$ref1[$k1];
  10. } else {
  11. $ref1[$k1][] = &$ref2[$k2];
  12. }
  13. } else {
  14. $ref2[$k2][1][] = $row['item3'];
  15. }
  16. }
  17. echo json_encode($result);

输出(带有一些换行和缩进以提高可读性):

  1. [
  2. ["a", ["c", ["h", "i"]], ["d", ["j", "k"]]],
  3. ["b", ["e", ["l", "m"]], ["f", ["n"]], ["g", ["o"]]]
  4. ]

以上片段可以通过在一组相似的条件内再次包装该过程来扩展以容纳每行4个元素。请注意(与第一个片段一样),只有最低级别的元素没有声明为引用。演示

  1. $items = [
  2. ['item1' => 'a', 'item2' => 'c', 'item3' => 'd2', 'item4' => 'f'],
  3. ['item1' => 'a', 'item2' => 'c', 'item3' => 'd2', 'item4' => 'g'],
  4. ['item1' => 'a', 'item2' => 'd', 'item3' => 'd3', 'item4' => 'h'],
  5. ['item1' => 'a', 'item2' => 'd', 'item3' => 'e', 'item4' => 'i'],
  6. ['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'j'],
  7. ['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'k']
  8. ];
  9. $result = [];
  10. foreach ($items as $row) {
  11. $k1 = $row['item1'];
  12. $k2 = $row['item1'] . '_' . $row['item2'];
  13. $k3 = $row['item1'] . '_' . $row['item2'] . '_' . $row['item3'];
  14. if (!isset($ref3[$k3])) {
  15. $ref3[$k3] = [$row['item3'], [$row['item4']]];
  16. if (!isset($ref2[$k2])) {
  17. $ref2[$k2] = [$row['item2'], &$ref3[$k3]];
  18. if (!isset($ref1[$k1])) {
  19. $ref1[$k1] = [$row['item1'], &$ref2[$k2]];
  20. $result[] = &$ref1[$k1];
  21. } else {
  22. $ref1[$k1][] = &$ref2[$k2];
  23. }
  24. } else {
  25. $ref2[$k2][1][] = &$ref3[$k3];
  26. }
  27. } else {
  28. $ref3[$k3][1][] = $row['item4'];
  29. }
  30. }
  31. echo json_encode($result);

输出:

  1. [
  2. ["a", ["c", ["d2", ["f", "g"]]], ["d", ["d3", ["h"], ["e", ["i"]]]]],
  3. ["b", ["c", ["e", ["j", "k"]]]]
  4. ]
英文:

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)

  1. $result = [];
  2. foreach ($items as $row) {
  3. $k1 = $row[&#39;item1&#39;];
  4. $k2 = $row[&#39;item1&#39;] . &#39;_&#39; . $row[&#39;item2&#39;];
  5. if (!isset($ref2[$k2])) {
  6. $ref2[$k2] = [$row[&#39;item2&#39;], [$row[&#39;item3&#39;]]];
  7. if (!isset($ref1[$k1])) {
  8. $ref1[$k1] = [$row[&#39;item1&#39;], &amp;$ref2[$k2]];
  9. $result[] = &amp;$ref1[$k1];
  10. } else {
  11. $ref1[$k1][] = &amp;$ref2[$k2];
  12. }
  13. } else {
  14. $ref2[$k2][1][] = $row[&#39;item3&#39;];
  15. }
  16. }
  17. echo json_encode($result);

Output (with some newlines and tabbing to improve readability):

  1. [
  2. [&quot;a&quot;,[&quot;c&quot;,[&quot;h&quot;,&quot;i&quot;]],[&quot;d&quot;,[&quot;j&quot;,&quot;k&quot;]]],
  3. [&quot;b&quot;,[&quot;e&quot;,[&quot;l&quot;,&quot;m&quot;]],[&quot;f&quot;,[&quot;n&quot;]],[&quot;g&quot;,[&quot;o&quot;]]]
  4. ]

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

  1. $items = [
  2. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;d2&#39;, &#39;item4&#39; =&gt; &#39;f&#39;],
  3. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;d2&#39;, &#39;item4&#39; =&gt; &#39;g&#39;],
  4. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;d&#39;, &#39;item3&#39; =&gt; &#39;d3&#39;, &#39;item4&#39; =&gt; &#39;h&#39;],
  5. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;d&#39;, &#39;item3&#39; =&gt; &#39;e&#39;, &#39;item4&#39; =&gt; &#39;i&#39;],
  6. [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;e&#39;, &#39;item4&#39; =&gt; &#39;j&#39;],
  7. [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;e&#39;, &#39;item4&#39; =&gt; &#39;k&#39;]
  8. ];
  9. $result = [];
  10. foreach ($items as $row) {
  11. $k1 = $row[&#39;item1&#39;];
  12. $k2 = $row[&#39;item1&#39;] . &#39;_&#39; . $row[&#39;item2&#39;];
  13. $k3 = $row[&#39;item1&#39;] . &#39;_&#39; . $row[&#39;item2&#39;] . &#39;_&#39; . $row[&#39;item3&#39;];
  14. if (!isset($ref3[$k3])) {
  15. $ref3[$k3] = [$row[&#39;item3&#39;], [$row[&#39;item4&#39;]]];
  16. if (!isset($ref2[$k2])) {
  17. $ref2[$k2] = [$row[&#39;item2&#39;], &amp;$ref3[$k3]];
  18. if (!isset($ref1[$k1])) {
  19. $ref1[$k1] = [$row[&#39;item1&#39;], &amp;$ref2[$k2]];
  20. $result[] = &amp;$ref1[$k1];
  21. } else {
  22. $ref1[$k1][] = &amp;$ref2[$k2];
  23. }
  24. } else {
  25. $ref2[$k2][1][] = &amp;$ref3[$k3];
  26. }
  27. } else {
  28. $ref3[$k3][1][] = $row[&#39;item4&#39;];
  29. }
  30. }
  31. echo json_encode($result);

Output:

  1. [
  2. [&quot;a&quot;,[&quot;c&quot;,[&quot;d2&quot;,[&quot;f&quot;,&quot;g&quot;]]],[&quot;d&quot;,[&quot;d3&quot;,[&quot;h&quot;],[&quot;e&quot;,[&quot;i&quot;]]]]],
  3. [&quot;b&quot;,[&quot;c&quot;,[&quot;e&quot;,[&quot;j&quot;,&quot;k&quot;]]]]
  4. ]

答案2

得分: 0

已解决!

  1. $items = [
  2. ['item1' => 'a', 'item2' => 'c', 'item3' => 'd2', 'item4' => 'f'],
  3. ['item1' => 'a', 'item2' => 'c', 'item3' => 'd1', 'item4' => 'g'],
  4. ['item1' => 'a', 'item2' => 'd', 'item3' => 'd3', 'item4' => 'h'],
  5. ['item1' => 'a', 'item2' => 'd', 'item3' => 'e', 'item4' => 'i'],
  6. ['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'j'],
  7. ['item1' => 'b', 'item2' => 'c', 'item3' => 'e', 'item4' => 'k'],
  8. ['item1' => 'b', 'item2' => 'c', 'item3' => 'e1', 'item4' => 'l'],
  9. ['item1' => 'b', 'item2' => 'c2', 'item3' => 'e', 'item4' => 'm'],
  10. ['item1' => 'x', 'item2' => 'c2', 'item3' => 'e', 'item4' => 'm'],
  11. ['item1' => 'x', 'item2' => 'c2', 'item3' => 'e2', 'item4' => 'n'],
  12. ];
  13. $items_new = [];
  14. $item1 = '';
  15. $count1 = 0;
  16. foreach ($items as $item):
  17. $item1_new = $item['item1'];
  18. $item2_new = $item['item2'];
  19. $item3_new = $item['item3'];
  20. $item4_new = $item['item4'];
  21. if ($item1 != $item1_new) {
  22. $items_new[] = [$item1_new, []];
  23. $item1 = $item1_new;
  24. $count1++;
  25. $item2 = '';
  26. $count2 = 0;
  27. }
  28. if ($item2 != $item2_new) {
  29. $items_new[$count1 - 1][$count2 + 1] = [$item2_new, []];
  30. $item2 = $item2_new;
  31. $count2++;
  32. $item3 = '';
  33. $count3 = 0;
  34. }
  35. if ($item3 != $item3_new) {
  36. $items_new[$count1 - 1][$count2 + 1 - 1][$count3 + 1] = [$item3_new, []];
  37. $item3 = $item3_new;
  38. $count3++;
  39. $item4 = '';
  40. $count4 = 0;
  41. }
  42. if ($item4 != $item4_new) {
  43. $items_new[$count1 - 1][$count2 + 1 - 1][$count3 + 1 - 1][$count4 + 1] = [$item4_new];
  44. $item4 = $item4_new;
  45. $count4++;
  46. }
  47. endforeach;
  48. foreach ($items_new as $items1) {
  49. if (is_string($items1)) {
  50. echo $items1 . '<br>';
  51. } else {
  52. foreach ($items1 as $items2) {
  53. if (is_string($items2)) {
  54. echo ' - ' . $items2 . '<br>';
  55. } else {
  56. foreach ($items2 as $items3) {
  57. if (is_string($items3)) {
  58. echo ' --- ' . $items3 . '<br>';
  59. } else {
  60. foreach ($items3 as $items4) {
  61. if (is_string($items4)) {
  62. echo ' ----- ' . $items4 . '<br>';
  63. } else {
  64. foreach ($items4 as $items5) {
  65. if (is_string($items5)) {
  66. echo ' ------- ' . $items5 . '<br>';
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }

以上是您提供的 PHP 代码的翻译部分。

英文:

Solved!

  1. $items = [
  2. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;d2&#39;, &#39;item4&#39; =&gt; &#39;f&#39;],
  3. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;d1&#39;, &#39;item4&#39; =&gt; &#39;g&#39;],
  4. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;d&#39;, &#39;item3&#39; =&gt; &#39;d3&#39;, &#39;item4&#39; =&gt; &#39;h&#39;],
  5. [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;d&#39;, &#39;item3&#39; =&gt; &#39;e&#39;, &#39;item4&#39; =&gt; &#39;i&#39;],
  6. [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;e&#39;, &#39;item4&#39; =&gt; &#39;j&#39;],
  7. [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;e&#39;, &#39;item4&#39; =&gt; &#39;k&#39;],
  8. [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;e1&#39;, &#39;item4&#39; =&gt; &#39;l&#39;],
  9. [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;c2&#39;, &#39;item3&#39; =&gt; &#39;e&#39;, &#39;item4&#39; =&gt; &#39;m&#39;],
  10. [&#39;item1&#39; =&gt; &#39;x&#39;, &#39;item2&#39; =&gt; &#39;c2&#39;, &#39;item3&#39; =&gt; &#39;e&#39;, &#39;item4&#39; =&gt; &#39;m&#39;],
  11. [&#39;item1&#39; =&gt; &#39;x&#39;, &#39;item2&#39; =&gt; &#39;c2&#39;, &#39;item3&#39; =&gt; &#39;e2&#39;, &#39;item4&#39; =&gt; &#39;n&#39;],
  12. ];
  13. $items_new = [];
  14. $item1 = &#39;&#39;;
  15. $count1 = 0;
  16. foreach ($items as $item):
  17. $item1_new = $item[&#39;item1&#39;];
  18. $item2_new = $item[&#39;item2&#39;];
  19. $item3_new = $item[&#39;item3&#39;];
  20. $item4_new = $item[&#39;item4&#39;];
  21. if($item1 != $item1_new)
  22. {
  23. $items_new[] = [$item1_new, []];
  24. $item1 = $item1_new;
  25. $count1++;
  26. $item2 = &#39;&#39;;
  27. $count2 = 0;
  28. }
  29. if($item2 != $item2_new)
  30. {
  31. $items_new[$count1 - 1][$count2+1] = [$item2_new, []] ;
  32. $item2 = $item2_new;
  33. $count2++;
  34. $item3 = &#39;&#39;;
  35. $count3 = 0;
  36. }
  37. if($item3 != $item3_new)
  38. {
  39. $items_new[$count1 - 1][$count2+1-1][$count3+1] = [$item3_new, []] ;
  40. $item3 = $item3_new;
  41. $count3++;
  42. $item4 = &#39;&#39;;
  43. $count4 = 0;
  44. }
  45. if($item4 != $item4_new)
  46. {
  47. $items_new[$count1 - 1][$count2+1-1][$count3+1-1][$count4+1] = [$item4_new] ;
  48. $item4 = $item4_new;
  49. $count4++;
  50. }
  51. endforeach;
  52. foreach($items_new as $items1)
  53. {
  54. if (is_string($items1))
  55. {
  56. echo $items1 . &#39;&lt;br&gt;&#39;;
  57. }
  58. else
  59. {
  60. foreach($items1 as $items2)
  61. {
  62. if (is_string($items2))
  63. {
  64. echo &#39; - &#39; . $items2 . &#39;&lt;br&gt;&#39;;
  65. }
  66. else
  67. {
  68. foreach ($items2 as $items3)
  69. {
  70. if (is_string($items3))
  71. {
  72. echo &#39; --- &#39; . $items3 . &#39;&lt;br&gt;&#39;;
  73. }
  74. else
  75. {
  76. foreach ($items3 as $items4)
  77. {
  78. if (is_string($items4))
  79. {
  80. echo &#39; ----- &#39; . $items4 . &#39;&lt;br&gt;&#39;;
  81. }
  82. else
  83. {
  84. foreach ($items4 as $items5)
  85. {
  86. if (is_string($items5))
  87. {
  88. echo &#39; ------- &#39; . $items5 . &#39;&lt;br&gt;&#39;;
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }

huangapple
  • 本文由 发表于 2023年5月29日 04:43:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定