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

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.

Answers


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


Need Your Help

How to create an activity for another other user with IBM OpenSocial

ibm opensocial

I'm trying to create a new activity programmatically (using a POST to the /activities/service/atom2/activities URL). This works fine if I'm authenticating with the desired user. Actually I do want to

Trigger.io - splash screen disappears, but webview does not load until I tap the screen

android kendo-ui trigger.io

I've got an android trigger.io app, which I'm using a custom splash screen on. Once this splash screen disappears, I sometimes am left with a black/grey gradient background while my webview has not

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.