Php 从json文件中存在的对象中提取值
我想从对象中获取值。应该从衬衫开始。它还应该检查它是否是mega_菜单。如果是大菜单,则显示其子菜单。也取决于megamenu的值,它应该应用类“parent”Php 从json文件中存在的对象中提取值,php,json,Php,Json,我想从对象中获取值。应该从衬衫开始。它还应该检查它是否是mega_菜单。如果是大菜单,则显示其子菜单。也取决于megamenu的值,它应该应用类“parent” { "1": { "title": "Root Catalog", "url": null, "id": "1", "img_url": null, "mega_menu": "true", "children": {
{
"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"
}
}
}
}
下面的代码将您的阵列转换为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;
//构建父ul
函数BuildMegaMenu($arr,$class='parent')
{
如果(!空($arr))
{
$html=“”;
外汇($arr作为$val)
{
if($val['mega_menu']&&&!empty($val['children'])//检查mega menu和children
{
//调用子助手函数以处理子对象的子对象
//在课堂上做一个ulli
$rs=SubBuildMegaMenu($val['children','parent');
如果(!空($rs)){
$html.=“- ”;
$html.=”;
$html.=$val['title'];
$html.=$rs;
$html.=”;
$html.=“
”;
}
}
否则{
$html.=“- ”$val['title']。“
”;
}
}
$html.=“
”;
返回$html;
}
返回false;
}
//用于处理子对象的子辅助函数
函数子BuildMegaMenu($arr,$class='parent')
{
如果(!空($arr))
{
$html=“”;
外汇($arr作为$val)
{
if($val['mega_menu']&&&!empty($val['children'])//检查mega menu和children
{
//调用子助手函数以处理子对象的子对象
//在课堂上做一个ulli
$rs=SubBuildMegaMenu($val['children',$class);
如果(!空($rs))
{
$html.=“- ”;
$html.=”;
$html.=$val['title'];
$html.=$rs;
$html.=”;
$html.=“
”;
}
}
否则{
$html.=“- ”$val['title']。“
”;
}
}
$html.=“
”;
返回$html;
}
}
$data={
"1": {
“标题”:“根目录”,
“url”:空,
“id”:“1”,
“img_url”:空,
“mega_菜单”:“正确”,
“儿童”:{
"2": {
“标题”:“默认类别”,
“url”:空,
“id”:“2”,
“img_url”:空,
“mega_菜单”:“正确”,
“儿童”:{
"4": {
“头衔”:“衬衫”,
“url”:“衬衫”,
“id”:“4”,
“img_url”:空,
“mega_菜单”:“错误”
},
"8": {
“标题”:“qme31q”,
“url”:“连衣裙”,
“id”:“8”,
“img_url”:空,
“mega_菜单”:“正确”,
“儿童”:{
"20": {
“标题”:“srtysryt”,
“url”:“srtysryt”,
“id”:“20”,
“img_url”:空,
“mega_菜单”:“错误”
}
}
},
"11": {
“标题”:“夹克+毛衣”,
“url”:“夹克衫”,
“id”:“11”,
“img_url”:空,
“mega_菜单”:“错误”
},
"12": {
“标题”:“附件”,
“url”:“附件”,
“id”:“12”,
“img_url”:空,
“mega_菜单”:“正确”,
“儿童”:{
"26": {
“标题”:“a”,
“url”:“a”,
“id”:“26”,
“img_url”:空,
“mega_菜单”:“错误”
}
}
},
"13": {
“头衔”:“加上”,
“url”:“加”,
“id”:“13”,
“img_url”:空,
“mega_菜单”:“错误”
},
"22": {
“头衔”:“abcde”,
“url”:“abcde”,
“id”:“22”,
“img_url”:空,
“mega_菜单”:“正确”,
“儿童”:{
"23": {
“标题”:“dduy”,
“url”:“dduy”,
“id”:“23”,
“img_url”:空,
“mega_菜单”:“错误”
}
}
},
"24": {
“标题”:“1”,
“url”:“1”,
“id”:“24”,
“img_url”:空,
“mega_菜单”:“正确”,
“儿童”:{
"25": {
“标题”:“1.1”,
“url”:“1-1”,
“id”:“25”,
“img_url”:空,
“mega_菜单”:“错误”
}
}
}
}
},
"16": {
“标题”:“测试”,
“url”:“测试”,
“id”:“16”,
“img_url”:空,
“mega_菜单”:“错误”
},
"18": {
“头衔”:“休息”,
“url”:“rest”,
“id”:“18”,
“img_url”:空,
“mega_菜单”:“错误”
},
"29": {
“标题”:“q”,