douwen2072 2016-11-25 11:25
浏览 37
已采纳

无法在php中获取嵌套数组的节点键名

I am trying to display nested array, and it prints whole array but unable to print node element like in our example [destinations],[QUOTATIONS]. i want to print array with this node elements.

actually i am generating tree view using this nested element that why i need to display node element also.

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  
                        )

                )

        )

)

here is my recursion code.

<?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条回答 默认 最新

  • dongshan8194 2016-11-25 11:41
    关注

    You can print node element in if block of you array checking. like:

    if (is_array($value)) {
                echo $key;//this will print the nodes(destinations,QUOTATIONS) .you cal add ul here
                traverseArray($value);
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探