创建多维数组嵌套列表 [英] Create nested list from Multidimensional Array

查看:132
本文介绍了创建多维数组嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中的数组,它看起来像这样:

 阵列(
    [0] =>阵列(
        [ID] => 1
        [标题] => 标题1
        [PARENT_ID] =>空值
        [深度] => 0
    )
    [1] =>阵列(
        [ID] => 2
        [标题] => 标题2
        [PARENT_ID] =>空值
        [深度] => 0
    )
    [2] =>阵列(
        [ID] => 3
        [标题] => 标题3
        [PARENT_ID] => 2
        [深度] => 1
    )
    [3] =>阵列(
        [ID] => 4
        [标题] => 标题4
        [PARENT_ID] => 2
        [深度] => 1
    )
    [4] =>阵列(
        [ID] =>五
        [标题] => 标题5
        [PARENT_ID] =>空值
        [深度] => 0
    )
    [5] =>阵列(
        [ID] => 6
        [标题] => 标题6
        [PARENT_ID] => 4
        [深度] => 2
    )

我想要做的就是遍历这个数组,并创建一个嵌套的<从中列表; OL过夜。所以结果应该是这样的:

 <&OL GT;
    <立GT;标题1 LT; /李> // ID = 1
    <立GT;标题2'; /李> // ID = 2
    <&OL GT;
        <立GT;标题3'; /李> // ID = 3 - > PARENT_ID = 2
        <立GT;标题4℃; /李> // ID = 4 - > PARENT_ID = 2
        <&OL GT;
            <立GT;标题6≤/李> // ID = 6 - > PARENT_ID = 4
        < / OL>
    < / OL>
    <立GT;标题5℃/李> // ID = 5
< / OL>

我一直在努力想办法如何我能得到这个工作。但到目前为止,所有的尝试失败了......

任何人任何想法如何,我可以创建这样一个嵌套的<&OL GT; 从这样的一个数组列表

请注意,我没有给定的数据的任何控制。我只是做一个API的调用,并返回JSON数据,我转换成数组。和数组看起来就像我所描述的。


解决方案

您应该使用递归

首先,在'PHP的语法的数组:

 < PHP
美元=阵列(
    0=>阵列(
        'ID'=> 1,
        '标题'=> 标题1
        PARENT_ID'=> '空值',
        深度=> 0
    )
    '1'=>阵列(
        'ID'=> 2,
        '标题'=> 标题2
        PARENT_ID'=> '空值',
        深度=> 0
    )
    '2'= GT;阵列(
        'ID'=> 3,
        '标题'=> 标题3
        PARENT_ID'=> 2,
        深度=> 1
    )
    '3'= GT;阵列(
        'ID'=> 4,
        '标题'=> 标题4,
        PARENT_ID'=> 2,
        深度=> 1
    )
    '4'= GT;阵列(
        'ID'=> 5,
        '标题'=> 标题5,
        PARENT_ID'=> '空值',
        深度=> 0
    )
    '5'= GT;阵列(
        'ID'=> 6,
        '标题'=> 标题6,
        PARENT_ID'=> 4,
        深度=> 0
    )
);

下面在 code

  $级别='NULL';函数r($ A,$级){
   $ R =<&OL GT;
   的foreach($ A作为I $){
       如果($ I ['PARENT_ID'] == $级别){
          $ R = $河<立GT; 。 $ I ['标题']。 R($ A,$ I ['身份证'])。 < /李>中;
       }
   }
   $ R = $河< / OL>中;
   返回$ R;
}打印R($ A,$级);?>

结果

 < OL><李>标题1< OL>< / OL>< /李><李>标题2'; OL><李>标题3' ;醇>
< / OL>< /李><李>标题4'; OL><李>标题6≤OL>< / OL>< /李>< / OL>< /李>< / OL>< /李><立GT;标题5
<&OL GT;< / OL>< /李>< / OL>

  1. 标题1 \\ n
  2. 标题2 \\ n
    1. 标题3 \\ n
    2. 标题4 \\ n
      1. 标题6 \\ n
  3. 标题5 \\ n

EDITED支票的解决方案之后

要避免空叶子:

 函数r($ A,$级){
   $ R ='';
   的foreach($ A作为I $){
       如果($ I ['PARENT_ID'] == $级别){
          $ R = $河<立GT; 。 $ I ['标题']。 R($ A,$ I ['身份证'])。 < /李>中;
       }
   }
   回报($ R =='''?':<&OL GT;$ R< / OL>);
}

I have an array in PHP, which looks like this:

array (
    [0] => array (
        [id] => 1
        [title] => "Title 1"
        [parent_id] => NULL
        [depth] => 0
    )
    [1] => array (
        [id] => 2
        [title] => "Title 2"
        [parent_id] => NULL
        [depth] => 0
    )
    [2] => array (
        [id] => 3
        [title] => "Title 3"
        [parent_id] => 2
        [depth] => 1
    )
    [3] => array (
        [id] => 4
        [title] => "Title 4"
        [parent_id] => 2
        [depth] => 1
    )
    [4] => array (
        [id] => 5
        [title] => "Title 5"
        [parent_id] => NULL
        [depth] => 0
    )
    [5] => array (
        [id] => 6
        [title] => "Title 6"
        [parent_id] => 4
        [depth] => 2
    )
)

What i want to do is iterate over this array and create a nested <ol> list from it. So the result should look like this:

<ol>
    <li>Title 1</li> // id = 1
    <li>Title 2</li> // id = 2
    <ol>
        <li>Title 3</li> // id = 3 -> parent_id = 2
        <li>Title 4</li> // id = 4 -> parent_id = 2
        <ol>
            <li>Title 6</li> // id = 6 -> parent_id = 4
        </ol>
    </ol>
    <li>Title 5</li> // id = 5
</ol>

I've been trying to think of a way how i could get this done. But so far every attempt failed...

Anyone any idea how i can create such a nested <ol> list from an array like that?

Please note that i do not have any control on the given data. I simply make a call to an API and it returns json data, which i convert to an array. And the array looks exactly like the one i described.

解决方案

You should use recursion:

First the array in 'php' syntax:

<?php
$a=array (
    '0' => array (
        'id' => 1,
        'title' => "Title 1",
        'parent_id' => 'NULL',
        'depth' => 0
    ),
    '1' => array (
        'id' => 2,
        'title' => "Title 2",
        'parent_id' => 'NULL',
        'depth' => 0
    ),
    '2' => array (
        'id' => 3,
        'title' => "Title 3",
        'parent_id' => 2,
        'depth' => 1
    ),
    '3' => array (
        'id' => 4,
        'title' => "Title 4",
        'parent_id' => 2,
        'depth' => 1
    ),
    '4' => array (
        'id' => 5,
        'title' => "Title 5",
        'parent_id' => 'NULL',
        'depth' => 0
    ),
    '5' => array (
        'id' => 6,
        'title' => "Title 6",
        'parent_id' => 4,
        'depth' => 0
    )
);

Here the code:

$level = 'NULL';

function r( $a, $level) {
   $r = "<ol>";
   foreach ( $a as $i ) {
       if ($i['parent_id'] == $level ) {
          $r = $r . "<li>" . $i['title'] . r( $a, $i['id'] ) . "</li>";
       }
   }
   $r = $r . "</ol>";
   return $r;
}

print r( $a, $level );

?>

The results:

<ol><li>Title 1<ol></ol></li><li>Title 2<ol><li>Title 3<ol>
</ol></li><li>Title 4<ol><li>Title 6<ol></ol></li></ol></li></ol></li><li>Title 5
<ol></ol></li></ol>

  1. Title 1\n

    1. Title 2\n

      1. Title 3\n

        1. Title 4\n

          1. Title 6\n

        2. Title 5\n

          EDITED AFTER CHECK AS SOLUTION

          To avoid empty leafs:

          function r( $a, $level) {
             $r = '' ;
             foreach ( $a as $i ) {
                 if ($i['parent_id'] == $level ) {
                    $r = $r . "<li>" . $i['title'] . r( $a, $i['id'] ) . "</li>";
                 }
             }
             return ($r==''?'':"<ol>". $r . "</ol>");
          }
          

          这篇关于创建多维数组嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

          查看全文
          登录 关闭
          扫码关注1秒登录
          发送“验证码”获取 | 15天全站免登陆