简体   繁体   English

递归数组菜单PHP

[英]recursive array menu php

How to build a php function that will convert this code into html menu this is the post that i make to the function via ajax 如何建立一个将这个代码转换成html菜单的php函数,这是我通过ajax对函数进行的发布

list[0][id] 55
list[1][id] 69
list[2][children][0][id]    67
list[2][children][1][id]    68
list[2][id] 57

And here's the array that i get into the function 这是我进入函数的数组

Array
(
    [0] => Array
        (
            [id] => 55
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 67
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 57
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 68
                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [id] => 69
                                        )

                                )

                        )

                )

        )

)

i've tryed this function but it doesn't work 我已经尝试过此功能,但是不起作用

function tomenu($arr){
  $html = '<ul>'.PHP_EOL;
  foreach ($arr as $v){
    $html .= '<li>' . $v['id'];
    if (array_key_exists('children', $v)){
      $html .= $this->tomenu($v['children']);
    }
    $html .= '</li>'.PHP_EOL;
  }
  $html .= '</ul>'.PHP_EOL;
  return $html;
}

Please help me. 请帮我。

function tomenu($arr) {
    $this->html .= '<ul>' . PHP_EOL;
    foreach ($arr as $v) {
        $this->html .= '<li>' . $v['id'];
        if (is_array($v['children'])) {
            $this->html .= $this->tomenu($v['children']);
        }
        $this->html .= '</li>' . PHP_EOL;
    }
    $this->html .= '</ul>' . PHP_EOL;
    $returnVal = $this->html;
    $this->html = '';
    return $returnVal;
}

i have not tested this code though 我虽然没有测试此代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM