多维arraylist

时间:2014-10-01 15:49:27

标签: php codeigniter recursion

有人可以帮我创建一个递归函数,在列表中打印出这个数组ul> li> ul.children等等。谢谢! :)

Array
(
    [0] => Array
        (
            [id] => 139
            [title] => Young People
            [pid] => 0
            [live] => 1
            [displayOrder] => 255
            [custom1] => 
            [custom2] => 
            [custom3] => 
            [live_id] => 
            [lastUpdated] => 1411839635
            [owner_id] => 1
            [availableFrom] => 1411772400
            [availableUntil] => 
            [type] => catTypeOne
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 143
                            [title] => Leadership & Development
                            [pid] => 139
                            [live] => 0
                            [displayOrder] => 255
                            [custom1] => 
                            [custom2] => 
                            [custom3] => 
                            [live_id] => 
                            [lastUpdated] => 1411839613
                            [owner_id] => 
                            [availableFrom] => 1411839613
                            [availableUntil] => 
                            [type] => 
                            [children] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 144
                            [title] => Cat 1
                            [pid] => 139
                            [live] => 0
                            [displayOrder] => 255
                            [custom1] => 
                            [custom2] => 
                            [custom3] => 
                            [live_id] => 
                            [lastUpdated] => 1411840264
                            [owner_id] => 
                            [availableFrom] => 1411840264
                            [availableUntil] => 
                            [type] => 
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 145
                                            [title] => Cat 2
                                            [pid] => 144
                                            [live] => 0
                                            [displayOrder] => 255
                                            [custom1] => 
                                            [custom2] => 
                                            [custom3] => 
                                            [live_id] => 
                                            [lastUpdated] => 1411840268
                                            [owner_id] => 
                                            [availableFrom] => 1411840268
                                            [availableUntil] => 
                                            [type] => 
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 146
                                                            [title] => Cat 3
                                                            [pid] => 145
                                                            [live] => 0
                                                            [displayOrder] => 255
                                                            [custom1] => 
                                                            [custom2] => 
                                                            [custom3] => 
                                                            [live_id] => 
                                                            [lastUpdated] => 1411840274
                                                            [owner_id] => 
                                                            [availableFrom] => 1411840274
                                                            [availableUntil] => 
                                                            [type] => 
                                                            [children] => Array
                                                                (
                                                                )

                                                        )

                                                    [1] => Array
                                                        (
                                                            [id] => 147
                                                            [title] => Cat 4
                                                            [pid] => 145
                                                            [live] => 0
                                                            [displayOrder] => 255
                                                            [custom1] => 
                                                            [custom2] => 
                                                            [custom3] => 
                                                            [live_id] => 
                                                            [lastUpdated] => 1411840281
                                                            [owner_id] => 
                                                            [availableFrom] => 1411840281
                                                            [availableUntil] => 
                                                            [type] => 
                                                            [children] => Array
                                                                (
                                                                )

                                                        )

                                                    [2] => Array
                                                        (
                                                            [id] => 148
                                                            [title] => Cat 5
                                                            [pid] => 145
                                                            [live] => 0
                                                            [displayOrder] => 255
                                                            [custom1] => 
                                                            [custom2] => 
                                                            [custom3] => 
                                                            [live_id] => 
                                                            [lastUpdated] => 1411840288
                                                            [owner_id] => 
                                                            [availableFrom] => 1411840288
                                                            [availableUntil] => 
                                                            [type] => 
                                                            [children] => Array
                                                                (
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 141
            [title] => Careers Guidance
            [pid] => 0
            [live] => 0
            [displayOrder] => 255
            [custom1] => 
            [custom2] => 
            [custom3] => 
            [live_id] => 
            [lastUpdated] => 1411839580
            [owner_id] => 
            [availableFrom] => 1411839580
            [availableUntil] => 
            [type] => catTypeOne
            [children] => Array
                (
                )

        )

)

2 个答案:

答案 0 :(得分:0)

这会使<ul>任意深入。每个<ul>都有一个level#类,其中#是一个从顶层开始的整数。

function make_list($array, $level = 0)
{
  $ret = "<ul class=\"level$level\">";
  foreach($array as $sub)
  {
    if(is_array($sub))
    {
      $sub = make_list($sub, $level + 1);
    }
    $ret .= "\n<li>$sub</li>";
  }
  $ret .= "\n</ul>";
  return $ret;
}

假设您的原始数组名为$my_array,您可以这样调用它:

echo make_list($my_array);

答案 1 :(得分:0)

只需创建一个显示一个级别的功能。每次遇到子数组时,都会在该子数组上递归调用该函数。每次调用函数之前和之后都要写ul标记。