2016-11-25 36 views
0

我試圖顯示嵌套數組,但它打印整個數組,但無法打印像我們的示例[目的地],[報價]中的節點元素。 我想用這個節點元素打印數組。無法獲取嵌套數組中的節點鍵名在php

實際上,我正在使用這個嵌套元素生成樹視圖,爲什麼我還需要顯示節點元素。

stdClass Object 
(
    [id] => 148 
    [status] => I 
    [consname] => juned ansari 
    [consusername] => junedconsultant 
    [agency_name] => mayur 
    [agency_username] => MayurMaroliya 
    [destinations] => Array 
     (
      [0] => stdClass Object 
       (
        [id] => 260 
        [from_date] => 2016-11-24 
        [to_date] => 2016-11-29 
        [country_id] => IN 
        [QUOTATIONS] => Array 
         (
           [id] => 260 
           [name] => ABC 
         ) 

       ) 

     ) 

) 

這裏是我的遞歸碼。

<?php 
function traverseArray($array) 
{ 
    // Loops through each element. If element again is array, function is recalled. If not, result is echoed. 
    foreach ($array as $key => $value) { 
     if (is_array($value)) { 
      traverseArray($value); 
     } else { 
      if (gettype($value) == 'object') { 
       echo "<ul>"; 
       traverseArray($value); 
      } else { 
       echo '<li><a href="#">' . $key . " : " . $value . '</a>'; 
      } 
     } 
    } 
} 
traverseArray($transition_data); 

回答

1

你可以在你的array checkingif版畫節點元素。如:

if (is_array($value)) { 
      echo $key;//this will print the nodes(destinations,QUOTATIONS) .you cal add ul here 
      traverseArray($value); 
     }