I am getting a nested array with children property from the WebSerice. I want to create the render able array so that I can use "drupal_render" function to render final HTML. I looked at the 'menu_tree_output' and 'taxonomy tree" that Drupal generates, but there is not proper documentation that mention how to structure the array to make it render able nested list.

Lets make it short: How do I restructure the final array that I get from Webservice so that I can make it renderable using drupal_render api. Final output is nested list(Folder tree).


What you are trying to solve is a recursive problem.... See below code which solves problem..

function listArrayRecursive($array_name, $ident = 0){
  if (is_array($array_name)){
    foreach ($array_name as $k => $v){
      if (is_array($v)){
        for ($i=0; $i < $ident * 10; $i++){ echo "&nbsp;"; }
        echo $k . " : " . "<br>";
        listArrayRecursive($v, $ident + 1);
        for ($i=0; $i < $ident * 10; $i++){ echo "&nbsp;"; }
        echo $k . " : " . $v . "<br>";
    echo "Variable = " . $array_name;

$ages = array(
        "ahmed" => "25",
        "mohamed" => "35",
        "group" => array("omar" => "15", "abdalla" => "20", "sub group" => array("john" => "10", "peter" => "20")));


Output will be

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.