Create renderable nested tree array like Menu Tree or Taxonomy 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 =&gt; $v){
      if (is_array($v)){
        for ($i=0; $i &lt; $ident * 10; $i++){ echo "&amp;nbsp;"; }
        echo $k . " : " . "&lt;br&gt;";
        listArrayRecursive($v, $ident + 1);
      }else{
        for ($i=0; $i &lt; $ident * 10; $i++){ echo "&amp;nbsp;"; }
        echo $k . " : " . $v . "&lt;br&gt;";
      }
    }
  }else{
    echo "Variable = " . $array_name;
  }
}

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

listArrayRecursive($ages);


Output will be