将json显示到树视图中

时间:2016-11-30 05:55:54

标签: php json

我正在尝试将json显示到树视图中但无法执行此操作 请帮我 下面是我的尝试 我的树生成但不正确只有数组转换为节点我觉得我在这段代码中遗漏了一些东西,请试着找出我的错误。

Json数据

{
    "lead_id": "SYS235",
    "agent_id": "30524",
    "user_id": "1",
    "raised_by": "C",
    "from_date": "2016-11-30",
    "to_date": "2016-12-03",
    "num_of_days": "3",
    "num_of_country": "1",
    "agent_contact": "9904773679",
    "currency_code": "INR",
    "created": "2016-11-30 10:36:29",
    "modified": "2016-11-30 10:36:29",
    "raised_by_note": "N/A",
    "num_of_rooms": "1",
    "noofadult": "1",
    "noofchild": "0",
    "pax_details": "[{\"title\":\"Mr\",\"name\":\"abc\"}]",
    "num_of_ebed_child": "0",
    "num_of_ebed_adlt": "0",
    "room_details": "[{\"noofadult\":\"1\",\"noofchild\":\"0\",\"child_ages\":[],\"num_of_ebed_adlt\":\"0\"}]",
    "package_category": "4,5",
    "id": "235",
    "status": "I",
    "branch_id": null,
    "branch_name": null,
    "consultant_id": "3045",
    "consname": "juned  ansari",
    "consusername": "junedconsultant",
    "lead_created_by": "SystemAdmin",
    "agency_name": "mayur",
    "agency_username": "MayurMaroliya",
    "destinations": [
        {
            "id": "317",
            "from_date": "2016-11-30",
            "to_date": "2016-12-03",
            "country_id": "IN",
            "city_id": "67798",
            "currency_code": "INR",
            "is_package": "0",
            "is_hotel": "1",
            "is_sight": "0",
            "is_transfer": "0",
            "notes": "ssss",
            "lead_id": "235",
            "assign_to": "3111",
            "status": "C",
            "country_name": "India",
            "city_name": "Saharanpur",
            "QUOTATIONS": [
                {
                    "id": "121",
                    "lead_destination_id": "317",
                    "user_id": "1",
                    "created": "2016-11-30 10:49:47",
                    "modified": "2016-11-30 10:49:47",
                    "quot_via": "0",
                    "inclusion": "<p>testing inclusion</p>",
                    "exclusion": "<p>test exclusion</p>",
                    "remarks": "<p>test remark</p>",
                    "itinerary": "<p>test itinerary</p>",
                    "is_final_quot": "0",
                    "systemusername": "SystemAdmin",
                    "fullname": "System Admin",
                    "Sight": [],
                    "Transfer": [],
                    "Hotel": {
                        "id": "81",
                        "lead_quot_id": "121",
                        "hotel_id": "511663",
                        "giata_id": "463954",
                        "notes": "testing",
                        "ratings": "3",
                        "address": "sdsd",
                        "supplier_code": "OS31",
                        "currency_code": "INR",
                        "conversion_rate": "1.000000",
                        "conversion_markup": "0.00",
                        "supplier_markup": "0",
                        "hotelname": "Hotel Suryalok",
                        "supplier_name": "nikhil patel (OS31 )"
                    },
                    "Rooms": [
                        {
                            "id": "97",
                            "quotation_hotel_id": "81",
                            "room_type_id": "2",
                            "no_of_adult": "1",
                            "no_of_child": "0",
                            "is_extra_adult_bed": "0",
                            "is_extra_child_bed": "0",
                            "adult_rate": "22",
                            "child_rate_with_bed": "0",
                            "child_rate_without_bed": "0",
                            "roomtype": "Luxury"
                        }
                    ],
                    "Meals": {
                        "breakfast": [
                            {
                                "id": "289",
                                "meal_type": "B",
                                "adult_rate": "222.0000",
                                "child_rate": "0.0000",
                                "meal_date": "2016-11-30 00:00:00",
                                "no_of_adult": "1",
                                "no_of_child": "0",
                                "lead_quot_id": "121",
                                "restaurant_name": "a",
                                "supplier_code": "OS28",
                                "currency_code": "KRW",
                                "conversion_rate": "0.056574",
                                "conversion_markup": "1.00",
                                "supplier_markup": "0",
                                "supplier_name": "vijay test (OS28 )"
                            }
                        ],
                        "lunch": [
                            {
                                "id": "290",
                                "meal_type": "L",
                                "adult_rate": "333.0000",
                                "child_rate": "0.0000",
                                "meal_date": "2016-12-01 00:00:00",
                                "no_of_adult": "1",
                                "no_of_child": "0",
                                "lead_quot_id": "121",
                                "restaurant_name": "b",
                                "supplier_code": "OS28",
                                "currency_code": "KRW",
                                "conversion_rate": "0.056574",
                                "conversion_markup": "1.00",
                                "supplier_markup": "0",
                                "supplier_name": "vijay test (OS28 )"
                            },
                            {
                                "id": "291",
                                "meal_type": "L",
                                "adult_rate": "555.0000",
                                "child_rate": "0.0000",
                                "meal_date": "2016-12-03 00:00:00",
                                "no_of_adult": "1",
                                "no_of_child": "0",
                                "lead_quot_id": "121",
                                "restaurant_name": "e",
                                "supplier_code": "OS28",
                                "currency_code": "KRW",
                                "conversion_rate": "0.056574",
                                "conversion_markup": "1.00",
                                "supplier_markup": "0",
                                "supplier_name": "vijay test (OS28 )"
                            }
                        ],
                        "dinner": [
                            {
                                "id": "292",
                                "meal_type": "D",
                                "adult_rate": "444.0000",
                                "child_rate": "0.0000",
                                "meal_date": "2016-12-02 00:00:00",
                                "no_of_adult": "1",
                                "no_of_child": "0",
                                "lead_quot_id": "121",
                                "restaurant_name": "c",
                                "supplier_code": "OS28",
                                "currency_code": "KRW",
                                "conversion_rate": "0.056574",
                                "conversion_markup": "1.00",
                                "supplier_markup": "0",
                                "supplier_name": "vijay test (OS28 )"
                            }
                        ]
                    }
                }
            ]
        }
    ]
}

这是我的PHP代码

<div class="container">
            <ul>
                <li>
                    <a href="#"><span style="font-weight: bold;color:green">Lead Info</span></a>
                    <ul>
                        <?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))
                                {
                                    //if(!empty($value)){
                                    echo '<li><a href="#"><span style="font-weight: bold;color:green">'. $key. '</span></a><ul>';
                                    traverseArray($value);
                                    //}else{
                                     //   echo '<li><a href="#">'. $key. " = <span style='color:orange;'>" . "" .'</span></a></li>';
                                    //}
                                }
                                else
                                {
                                    if(gettype($value)=='object'){
                                        traverseArray($value);
                                    }else{ 
                                        echo '<li><a href="#">'. $key. " = <span style='color:orange;'>" . $value .'</span></a></li>';
                                    }
                                }
                            }
                        }
                        traverseArray($transition_data);
                        ?>
                    </ul>  
                </li>     
            </ul>    
        </div>

控制器

public function historydataAction() {
        $this->layout('layout/auto');
        return new ViewModel(array("transition_data"=> (array)json_decode('{"lead_id":"SYS235","agent_id":"30524","user_id":"1","raised_by":"C","from_date":"2016-11-30","to_date":"2016-12-03","num_of_days":"3","num_of_country":"1","agent_contact":"9904773679","currency_code":"INR","created":"2016-11-30 10:36:29","modified":"2016-11-30 10:36:29","raised_by_note":"N\/A","num_of_rooms":"1","noofadult":"1","noofchild":"0","pax_details":"[{\"title\":\"Mr\",\"name\":\"abc\"}]","num_of_ebed_child":"0","num_of_ebed_adlt":"0","room_details":"[{\"noofadult\":\"1\",\"noofchild\":\"0\",\"child_ages\":[],\"num_of_ebed_adlt\":\"0\"}]","package_category":"4,5","id":"235","status":"I","branch_id":null,"branch_name":null,"consultant_id":"3045","consname":"juned  ansari","consusername":"junedconsultant","lead_created_by":"SystemAdmin","agency_name":"mayur","agency_username":"MayurMaroliya","destinations":[{"id":"317","from_date":"2016-11-30","to_date":"2016-12-03","country_id":"IN","city_id":"67798","currency_code":"INR","is_package":"0","is_hotel":"1","is_sight":"0","is_transfer":"0","notes":"ssss","lead_id":"235","assign_to":"3111","status":"C","country_name":"India","city_name":"Saharanpur","QUOTATIONS":[{"id":"121","lead_destination_id":"317","user_id":"1","created":"2016-11-30 10:49:47","modified":"2016-11-30 10:49:47","quot_via":"0","inclusion":"<p>testing inclusion<\/p>","exclusion":"<p>test exclusion<\/p>","remarks":"<p>test remark<\/p>","itinerary":"<p>test itinerary<\/p>","is_final_quot":"0","systemusername":"SystemAdmin","fullname":"System Admin","Sight":[],"Transfer":[],"Hotel":{"id":"81","lead_quot_id":"121","hotel_id":"511663","giata_id":"463954","notes":"testing","ratings":"3","address":"sdsd","supplier_code":"OS31","currency_code":"INR","conversion_rate":"1.000000","conversion_markup":"0.00","supplier_markup":"0","hotelname":"Hotel Suryalok","supplier_name":"nikhil patel (OS31 )"},"Rooms":[{"id":"97","quotation_hotel_id":"81","room_type_id":"2","no_of_adult":"1","no_of_child":"0","is_extra_adult_bed":"0","is_extra_child_bed":"0","adult_rate":"22","child_rate_with_bed":"0","child_rate_without_bed":"0","roomtype":"Luxury"}],"Meals":{"breakfast":[{"id":"289","meal_type":"B","adult_rate":"222.0000","child_rate":"0.0000","meal_date":"2016-11-30 00:00:00","no_of_adult":"1","no_of_child":"0","lead_quot_id":"121","restaurant_name":"a","supplier_code":"OS28","currency_code":"KRW","conversion_rate":"0.056574","conversion_markup":"1.00","supplier_markup":"0","supplier_name":"vijay test (OS28 )"}],"lunch":[{"id":"290","meal_type":"L","adult_rate":"333.0000","child_rate":"0.0000","meal_date":"2016-12-01 00:00:00","no_of_adult":"1","no_of_child":"0","lead_quot_id":"121","restaurant_name":"b","supplier_code":"OS28","currency_code":"KRW","conversion_rate":"0.056574","conversion_markup":"1.00","supplier_markup":"0","supplier_name":"vijay test (OS28 )"},{"id":"291","meal_type":"L","adult_rate":"555.0000","child_rate":"0.0000","meal_date":"2016-12-03 00:00:00","no_of_adult":"1","no_of_child":"0","lead_quot_id":"121","restaurant_name":"e","supplier_code":"OS28","currency_code":"KRW","conversion_rate":"0.056574","conversion_markup":"1.00","supplier_markup":"0","supplier_name":"vijay test (OS28 )"}],"dinner":[{"id":"292","meal_type":"D","adult_rate":"444.0000","child_rate":"0.0000","meal_date":"2016-12-02 00:00:00","no_of_adult":"1","no_of_child":"0","lead_quot_id":"121","restaurant_name":"c","supplier_code":"OS28","currency_code":"KRW","conversion_rate":"0.056574","conversion_markup":"1.00","supplier_markup":"0","supplier_name":"vijay test (OS28 )"}]}}]}]}')));
    }

1 个答案:

答案 0 :(得分:-1)

<div class="container">
                <ul>
                    <li>
                        <a href="#"><span style="font-weight: bold;color:green">Lead Info</span></a>
                        <ul>
                            <?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))
                                    {
                                        if(!empty($value)){
                                        echo '<li><a href="#"><span style="font-weight: bold;color:green">'. $key. '</span></a><ul>';
                                        traverseArray($value);
                                        echo "</ul>";
                                        }else{
                                            echo '<li><a href="#">'. $key. " = <span style='color:orange;'>" . "" .'</span></a></li>';
                                        }
                                    }
                                    else
                                    {
                                        if(gettype($value)=='object'){
                                            traverseArray($value);
                                        }else{ 
                                            echo '<li><a href="#">'. $key. " = <span style='color:orange;'>" . $value .'</span></a></li>';
                                        }
                                    }
                                }
                            }
                            traverseArray($transition_data);
                            ?>
                        </ul>  
                    </li>     
                </ul>    
            </div>