Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php和mysql的嵌套json_Php_Mysql_Json - Fatal编程技术网

使用php和mysql的嵌套json

使用php和mysql的嵌套json,php,mysql,json,Php,Mysql,Json,我有一个名为ecomm的主类别和3个子类别,每个子类别还有两个子类别如何以子格式显示结果 我的输出像 ecomm { men{ shirts{ p1, p2} } women { tops {p1, p2} } Kids{ shoes{p1,p2} } } 这是您的解决方案,请参阅下面的代码,并检查此 <?php $data=array(); $all = array( array('id'=>1,

我有一个名为ecomm的主类别和3个子类别,每个子类别还有两个子类别如何以子格式显示结果 我的输出像

ecomm
{
 men{  
     shirts{ p1, p2}
  }
women {   tops {p1, p2}
     } 
Kids{
     shoes{p1,p2}
  }
  }

这是您的解决方案,请参阅下面的代码,并检查此

<?php 
$data=array();

$all  = array(
            array('id'=>1,'ParentID'=>'0','type'=>'Men'),
            array('id'=>2,'ParentID'=>'0','type'=>'Women'),
            array('id'=>3,'ParentID'=>'0','type'=>'Kids'),
            array('id'=>4,'ParentID'=>'1','type'=>'Shirt'),
            array('id'=>5,'ParentID'=>'2','type'=>'Top'),
            array('id'=>6,'ParentID'=>'3','type'=>'Shoes'),
            array('id'=>7,'ParentID'=>'4','type'=>'p1'),
            array('id'=>8,'ParentID'=>'5','type'=>'p1'),
            array('id'=>9,'ParentID'=>'6','type'=>'p1'),
            array('id'=>10,'ParentID'=>'4','type'=>'p2'),
            array('id'=>11,'ParentID'=>'5','type'=>'p2'),
            array('id'=>12,'ParentID'=>'6','type'=>'p2')
        );

foreach($all as $key => $val)
{
    if($val['ParentID']==0)
    {
        $data[$key]=$val;

        foreach($all as $k => $v)
        { 
            if($val['id'] == $v['ParentID']){
                $data[$key]['subBranches'][$key]= $v;
                foreach($all as $a => $s)
                { 
                    if($v['id'] == $s['ParentID']){
                        $data[$key]['subBranches'][$key]['sub_subBranches'][]= $s;
                    }
                }
            }
        }
    }
}
echo json_encode($data);
?>

从何处获取结果,是db结果集还是xml数据?要了解json是如何格式化的,只需创建一个具有所需结构的数组,然后传递它。然后只需回显字符串。到目前为止您尝试了什么??与我们共享您的代码。。。这样社区就能帮助你。。
[
  {
    "id":1,
    "ParentID":"0",
    "type":"Men",
    "subBranches":[
      {
        "id":4,
        "ParentID":"1",
        "type":"Shirt",
        "sub_subBranches":[
          {
            "id":7,
            "ParentID":"4",
            "type":"p1"
          },
          {
            "id":10,
            "ParentID":"4",
            "type":"p2"
          }
        ]
      }
    ]
  },
  {
    "id":2,
    "ParentID":"0",
    "type":"Women",
    "subBranches":{
      "1":{
        "id":5,
        "ParentID":"2",
        "type":"Top",
        "sub_subBranches":[
          {
            "id":8,
            "ParentID":"5",
            "type":"p1"
          },
          {
            "id":11,
            "ParentID":"5",
            "type":"p2"
          }
        ]
      }
    }
  },
  {
    "id":3,
    "ParentID":"0",
    "type":"Kids",
    "subBranches":{
      "2":{
        "id":6,
        "ParentID":"3",
        "type":"Shoes",
        "sub_subBranches":[
          {
            "id":9,
            "ParentID":"6",
            "type":"p1"
          },
          {
            "id":12,
            "ParentID":"6",
            "type":"p2"
          }
        ]
      }
    }
  }
]