从json文件中存在的对象中提取值

时间:2017-05-27 09:35:56

标签: php json

我想从对象中获取值。它应该从衬衫开始。它还应该检查它是否是mega_menu。如果是超级菜单,请展示它的孩子。也取决于megamenu值,它应该应用class' parent'。

{
    "1": {
        "title": "Root Catalog",
        "url": null,
        "id": "1",
        "img_url": null,
        "mega_menu": "true",
        "children": {
            "2": {
                "title": "Default Category",
                "url": null,
                "id": "2",
                "img_url": null,
                "mega_menu": "true",
                "children": {
                    "4": {
                        "title": "shirts",
                        "url": "shirts",
                        "id": "4",
                        "img_url": null,
                        "mega_menu": "false"
                    },
                    "8": {
                        "title": "qme31q",
                        "url": "dresses",
                        "id": "8",
                        "img_url": null,
                        "mega_menu": "true",
                        "children": {
                            "20": {
                                "title": "srtysryt",
                                "url": "srtysryt",
                                "id": "20",
                                "img_url": null,
                                "mega_menu": "false"
                            }
                        }
                    },
                    "11": {
                        "title": "Jackets + Sweaters",
                        "url": "jackets-sweaters",
                        "id": "11",
                        "img_url": null,
                        "mega_menu": "false"
                    },
                    "12": {
                        "title": "Accessories",
                        "url": "accessories",
                        "id": "12",
                        "img_url": null,
                        "mega_menu": "true",
                        "children": {
                            "26": {
                                "title": "a",
                                "url": "a",
                                "id": "26",
                                "img_url": null,
                                "mega_menu": "false"
                            }
                        }
                    },
                    "13": {
                        "title": "Plus",
                        "url": "plus",
                        "id": "13",
                        "img_url": null,
                        "mega_menu": "false"
                    },
                    "22": {
                        "title": "abcde",
                        "url": "abcde",
                        "id": "22",
                        "img_url": null,
                        "mega_menu": "true",
                        "children": {
                            "23": {
                                "title": "dduy",
                                "url": "dduy",
                                "id": "23",
                                "img_url": null,
                                "mega_menu": "false"
                            }
                        }
                    },
                    "24": {
                        "title": "1",
                        "url": "1",
                        "id": "24",
                        "img_url": null,
                        "mega_menu": "true",
                        "children": {
                            "25": {
                                "title": "1.1",
                                "url": "1-1",
                                "id": "25",
                                "img_url": null,
                                "mega_menu": "false"
                            }
                        }
                    }
                }
            },
            "16": {
                "title": "test",
                "url": "test",
                "id": "16",
                "img_url": null,
                "mega_menu": "false"
            },
            "18": {
                "title": "rest",
                "url": "rest",
                "id": "18",
                "img_url": null,
                "mega_menu": "false"
            },
            "29": {
                "title": "q",
                "url": "q",
                "id": "29",
                "img_url": null,
                "mega_menu": "false"
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

下面的代码将您的数组转换为Ul li列表。

// Build parent ul
function BuildMegaMenu($arr,$class = 'parent')
{
    if(!empty($arr))
    {
        $html = "<ul class='p_category'>";
        foreach($arr as $val)
        {
            if($val['mega_menu'] && !empty($val['children'])) //  check for mega menu and children
            {
                // call sub helper function for procesing the children of children
                // making a ul li with class
                $rs = SubBuildMegaMenu($val['children'], 'parent'); 
                if(!empty($rs)) {
                    $html .= "<li>";
                        $html .= "<div class='".$class."'>";
                            $html .= $val['title'];
                            $html .= $rs;
                        $html .= "</div>";  
                    $html .= "</li>";
                }
            }
            else {
                $html .= "<li><div class='parent'>".$val['title']."</div></li>";
            }
        }
        $html .= "</ul>";
        return $html;
    }
    return false;
}

// Sub helper function for processing childrens
function SubBuildMegaMenu($arr, $class = 'parent')
{
    if(!empty($arr))
    {
        $html = "<ul>";
        foreach($arr as $val)
        {
            if($val['mega_menu'] && !empty($val['children'])) //  check for mega menu and children
            {
                // call sub helper function for procesing the children of children
                // making a ul li with class
                $rs = SubBuildMegaMenu($val['children'], $class);
                if(!empty($rs))
                {
                    $html .= "<li>";
                        $html .= "<div class='".$class."'>";
                            $html .= $val['title'];
                            $html .= $rs;
                        $html .= "</div>";  
                    $html .= "</li>";
                }
            }
            else{
                $html .= "<li><div class='parent'>".$val['title']."</div></li>";
            }
        }
        $html .= "</ul>";
        return  $html;
    }
}
$data = '{
"1": {
    "title": "Root Catalog",
    "url": null,
    "id": "1",
    "img_url": null,
    "mega_menu": "true",
    "children": {
        "2": {
            "title": "Default Category",
            "url": null,
            "id": "2",
            "img_url": null,
            "mega_menu": "true",
            "children": {
                "4": {
                    "title": "shirts",
                    "url": "shirts",
                    "id": "4",
                    "img_url": null,
                    "mega_menu": "false"
                },
                "8": {
                    "title": "qme31q",
                    "url": "dresses",
                    "id": "8",
                    "img_url": null,
                    "mega_menu": "true",
                    "children": {
                        "20": {
                            "title": "srtysryt",
                            "url": "srtysryt",
                            "id": "20",
                            "img_url": null,
                            "mega_menu": "false"
                        }
                    }
                },
                "11": {
                    "title": "Jackets + Sweaters",
                    "url": "jackets-sweaters",
                    "id": "11",
                    "img_url": null,
                    "mega_menu": "false"
                },
                "12": {
                    "title": "Accessories",
                    "url": "accessories",
                    "id": "12",
                    "img_url": null,
                    "mega_menu": "true",
                    "children": {
                        "26": {
                            "title": "a",
                            "url": "a",
                            "id": "26",
                            "img_url": null,
                            "mega_menu": "false"
                        }
                    }
                },
                "13": {
                    "title": "Plus",
                    "url": "plus",
                    "id": "13",
                    "img_url": null,
                    "mega_menu": "false"
                },
                "22": {
                    "title": "abcde",
                    "url": "abcde",
                    "id": "22",
                    "img_url": null,
                    "mega_menu": "true",
                    "children": {
                        "23": {
                            "title": "dduy",
                            "url": "dduy",
                            "id": "23",
                            "img_url": null,
                            "mega_menu": "false"
                        }
                    }
                },
                "24": {
                    "title": "1",
                    "url": "1",
                    "id": "24",
                    "img_url": null,
                    "mega_menu": "true",
                    "children": {
                        "25": {
                            "title": "1.1",
                            "url": "1-1",
                            "id": "25",
                            "img_url": null,
                            "mega_menu": "false"
                        }
                    }
                }
            }
        },
        "16": {
            "title": "test",
            "url": "test",
            "id": "16",
            "img_url": null,
            "mega_menu": "false"
        },
        "18": {
            "title": "rest",
            "url": "rest",
            "id": "18",
            "img_url": null,
            "mega_menu": "false"
        },
        "29": {
            "title": "q",
            "url": "q",
            "id": "29",
            "img_url": null,
            "mega_menu": "false"
        }
    }
}}';
$convertedArr = json_decode($data, true);
echo '<pre>';print_r(BuildMegaMenu($convertedArr));die;