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

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

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

问题

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

我有这样的数据:

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

这也可以看作是这个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:

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

Which can also be seen as this PHP array:

$items = [
    [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;h&#39;],
    [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;c&#39;, &#39;item3&#39; =&gt; &#39;i&#39;],
    [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;d&#39;, &#39;item3&#39; =&gt; &#39;j&#39;],
    [&#39;item1&#39; =&gt; &#39;a&#39;, &#39;item2&#39; =&gt; &#39;d&#39;, &#39;item3&#39; =&gt; &#39;k&#39;],
    [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;e&#39;, &#39;item3&#39; =&gt; &#39;l&#39;],
    [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;e&#39;, &#39;item3&#39; =&gt; &#39;m&#39;],
    [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;f&#39;, &#39;item3&#39; =&gt; &#39;n&#39;],
    [&#39;item1&#39; =&gt; &#39;b&#39;, &#39;item2&#39; =&gt; &#39;g&#39;, &#39;item3&#39; =&gt; &#39;o&#39;],
];

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.

&lt;?php

$items = [
    [&#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;],
    [&#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;],
    [&#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;],
    [&#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;],
    [&#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;],
    [&#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;],
    [&#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;],
    [&#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;],
];


        $items_new = [];

        $item1 = &#39;&#39;;
        $item2 = &#39;&#39;;
        $item3 = &#39;&#39;;
        $item4 = &#39;&#39;;

        foreach ($items as $item):

          $item1_new = $item[&#39;item1&#39;];
          $item2_new = $item[&#39;item2&#39;];
          $item3_new = $item[&#39;item3&#39;];
          $item4_new = $item[&#39;item4&#39;];

          if($item1 != $item1_new)
          {
            $items_new[] = [$item1_new, []];

            $item1 = $item1_new;

            $item2 = &#39;&#39;;
          }
          
          if($item2 != $item2_new)
          {
            $items_new[count($items_new) - 1][] = [$item2_new, []];

            $item2 = $item2_new;

            $item3 = &#39;&#39;;
          }

          if($item3 != $item3_new)
          {
            $items_new[count($items_new) - 1][count($items_new[1]) - 1][] = [$item3_new, []];

            $item3 = $item3_new;

            $item4 = &#39;&#39;;
          }

          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 . &#39;&lt;br&gt;&#39;;
          }
          elseif (is_array($items1))
          {
            foreach($items1 as $items2)
            {
                if (is_string($items2))
                {
                  echo  &#39; - &#39; . $items2 . &#39;&lt;br&gt;&#39;;
                }
                elseif (is_array($items2))
                {
                  foreach ($items2 as $items3)
                  {
                    if (is_string($items3))
                    {
                      echo  &#39; --- &#39; . $items3 . &#39;&lt;br&gt;&#39;;
                    }
                    elseif (is_array($items3))
                    {
                      foreach ($items3 as $items4)
                      {
                        if (is_string($items4))
                        {
                          echo  &#39; ----- &#39; . $items4 . &#39;&lt;br&gt;&#39;;
                        }
                        elseif (is_array($items4))
                        {
                          foreach ($items4 as $items5)
                          {
                            if (is_string($items5))
                            {
                              echo  &#39; ------- &#39; . $items5 . &#39;&lt;br&gt;&#39;;
                            }
                            elseif (is_array($items5))
                            {
                              foreach ($items5 as $items6)
                              {
                                
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
            }
          }
        }
        ?&gt;

答案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[&#39;item1&#39;];
    $k2 = $row[&#39;item1&#39;] . &#39;_&#39; . $row[&#39;item2&#39;];
    if (!isset($ref2[$k2])) {
        $ref2[$k2] = [$row[&#39;item2&#39;], [$row[&#39;item3&#39;]]];
        if (!isset($ref1[$k1])) {
            $ref1[$k1] = [$row[&#39;item1&#39;], &amp;$ref2[$k2]];
            $result[] = &amp;$ref1[$k1];
        } else {
            $ref1[$k1][] = &amp;$ref2[$k2];
        }
    } else {
        $ref2[$k2][1][] = $row[&#39;item3&#39;];
    }
}
echo json_encode($result);

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

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

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 = [
    [&#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;],
    [&#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;],
    [&#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;],
    [&#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;],
    [&#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;],
    [&#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;]
];

$result = [];
foreach ($items as $row) {
    $k1 = $row[&#39;item1&#39;];
    $k2 = $row[&#39;item1&#39;] . &#39;_&#39; . $row[&#39;item2&#39;];
    $k3 = $row[&#39;item1&#39;] . &#39;_&#39; . $row[&#39;item2&#39;] . &#39;_&#39; . $row[&#39;item3&#39;];
    if (!isset($ref3[$k3])) {
        $ref3[$k3] = [$row[&#39;item3&#39;], [$row[&#39;item4&#39;]]];
        if (!isset($ref2[$k2])) {
            $ref2[$k2] = [$row[&#39;item2&#39;], &amp;$ref3[$k3]];
            if (!isset($ref1[$k1])) {
                $ref1[$k1] = [$row[&#39;item1&#39;], &amp;$ref2[$k2]];
                $result[] = &amp;$ref1[$k1];
            } else {
                $ref1[$k1][] = &amp;$ref2[$k2];
            }
        } else {
            $ref2[$k2][1][] = &amp;$ref3[$k3];
        }
    } else {
        $ref3[$k3][1][] = $row[&#39;item4&#39;];
    }
}
echo json_encode($result);

Output:

[
    [&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;]]]]],
    [&quot;b&quot;,[&quot;c&quot;,[&quot;e&quot;,[&quot;j&quot;,&quot;k&quot;]]]]
]

答案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 = [
[&#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;],
[&#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;],
[&#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;],
[&#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;],
[&#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;],
[&#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;],
[&#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;],
[&#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;],
[&#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;],
[&#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;],
];
$items_new = [];
$item1 = &#39;&#39;;
$count1 = 0;
foreach ($items as $item):
$item1_new = $item[&#39;item1&#39;];
$item2_new = $item[&#39;item2&#39;];
$item3_new = $item[&#39;item3&#39;];
$item4_new = $item[&#39;item4&#39;];
if($item1 != $item1_new)
{
$items_new[] = [$item1_new, []];
$item1 = $item1_new;
$count1++;
$item2 = &#39;&#39;;
$count2 = 0;
}
if($item2 != $item2_new)
{
$items_new[$count1 - 1][$count2+1] = [$item2_new, []] ;
$item2 = $item2_new;
$count2++;
$item3 = &#39;&#39;;
$count3 = 0;
}
if($item3 != $item3_new)
{
$items_new[$count1 - 1][$count2+1-1][$count3+1] = [$item3_new, []] ;
$item3 = $item3_new;
$count3++;
$item4 = &#39;&#39;;
$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 . &#39;&lt;br&gt;&#39;;
}
else
{
foreach($items1 as $items2)
{
if (is_string($items2))
{
echo  &#39; - &#39; . $items2 . &#39;&lt;br&gt;&#39;;
}
else
{
foreach ($items2 as $items3)
{
if (is_string($items3))
{
echo  &#39; --- &#39; . $items3 . &#39;&lt;br&gt;&#39;;
}
else
{
foreach ($items3 as $items4)
{
if (is_string($items4))
{
echo  &#39; ----- &#39; . $items4 . &#39;&lt;br&gt;&#39;;
}
else
{
foreach ($items4 as $items5)
{
if (is_string($items5))
{
echo  &#39; ------- &#39; . $items5 . &#39;&lt;br&gt;&#39;;
}
}
}
}
}
}
}
}
}
}

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:

确定