对象数组内的对象数组

时间:2016-04-07 12:06:55

标签: php json

我想将一个名为subbranches的对象数组发送到相同对象的主数组中

来自同一张桌子

我有一个包含Main和Sub分支的表 我需要在主对象的数组中添加子数据,并且应该调用该数组(SubBranches)

我如何获得此JSON:

[
  {
    "ServiceTypeID": 3,
    "EServiceTypeName": "Electronic Devices",
    "PicturePath": "localhost/adminctrl/servicetypes/3.png",
    "ParentID": 0,
    "createdAt": "2016-03-03",
    "updatedAt": "2016-03-03",
    "ChildCount": 0,
    "IsSelect": 0,
    "subBranches": [
      {
        "ServiceTypeID": 13,
        "EServiceTypeName": "sub Electronic Devices",
        "PicturePath": "localhost/adminctrl/servicetypes/13.png",
        "ParentID": 3,
        "createdAt": "2016-03-03",
        "updatedAt": "2016-03-03",
        "ChildCount": 0,
        "IsSelect": 0
      },
      {
        "ServiceTypeID": 14,
        "EServiceTypeName": "sub Electronic Devices",
        "PicturePath": "localhost/adminctrl/servicetypes/14.png",
        "ParentID": 3,
        "createdAt": "2016-03-03",
        "updatedAt": "2016-03-03",
        "ChildCount": 0,
        "IsSelect": 0
      },
      {
        "ServiceTypeID": 15,
        "EServiceTypeName": "sub Electronic Devices",
        "PicturePath": "localhost/adminctrl/servicetypes/15.png",
        "ParentID": 3,
        "createdAt": "2016-03-03",
        "updatedAt": "2016-03-03",
        "ChildCount": 0,
        "IsSelect": 0
      }
    ]
  },



  {
    "ServiceTypeID": 11,
    "EServiceTypeName": "Paint",
    "PicturePath": "localhost/adminctrl/servicetypes/11.png",
    "ParentID": 0,
    "createdAt": "2016-03-15",
    "updatedAt": "2016-03-15",
    "ChildCount": 0,
    "IsSelect": 0,
    "subBranches": [
      {
        "ServiceTypeID": 30,
        "EServiceTypeName": "sub Pain",
        "PicturePath": "localhost/adminctrl/servicetypes/13.png",
        "ParentID": 11,
        "createdAt": "2016-03-03",
        "updatedAt": "2016-03-03",
        "ChildCount": 0,
        "IsSelect": 0
      },
      {
        "ServiceTypeID": 31,
        "EServiceTypeName": "sub Pain",
        "PicturePath": "localhost/adminctrl/servicetypes/14.png",
        "ParentID": 11,
        "createdAt": "2016-03-03",
        "updatedAt": "2016-03-03",
        "ChildCount": 0,
        "IsSelect": 0
      },
      {
        "ServiceTypeID": 32,
        "EServiceTypeName": "sub Pain",
        "PicturePath": "localhost/adminctrl/servicetypes/15.png",
        "ParentID": 1,
        "createdAt": "2016-03-03",
        "updatedAt": "2016-03-03",
        "ChildCount": 0,
        "IsSelect": 0
      }
    ]
  },



  {
    "ServiceTypeID": 8,
    "EServiceTypeName": "Carpenter",
    "PicturePath": "localhost/adminctrl/servicetypes/8.png",
    "ParentID": 0,
    "createdAt": "2016-03-15",
    "updatedAt": "2016-03-15",
    "ChildCount": 0,
    "IsSelect": 0,
    "subBranches": [
      {
        "ServiceTypeID": 20,
        "EServiceTypeName": "sub Carpenter",
        "PicturePath": "localhost/adminctrl/servicetypes/20.png",
        "ParentID": 8,
        "createdAt": "2016-03-03",
        "updatedAt": "2016-03-03",
        "ChildCount": 0,
        "IsSelect": 0
      },
      {
        "ServiceTypeID": 21,
        "EServiceTypeName": "sub Carpenter",
        "PicturePath": "localhost/adminctrl/servicetypes/21.png",
        "ParentID": 8,
        "createdAt": "2016-03-03",
        "updatedAt": "2016-03-03",
        "ChildCount": 0,
        "IsSelect": 0
      },
      {
        "ServiceTypeID": 22,
        "EServiceTypeName": "sub Carpenter",
        "PicturePath": "localhost/adminctrl/servicetypes/22.png",
        "ParentID": 8,
        "createdAt": "2016-03-03",
        "updatedAt": "2016-03-03",
        "ChildCount": 0,
        "IsSelect": 0
      }
    ]
  }
]

我如何使用php获得相同的json 我使用这个功能让json像我的putit一样轻拍

function action_getall2(){
    global $db;
    global $table_name;
    $all = $db->get($table_name);
    $data=array();
    // $data2 = array();

    $pcnt=0;
    $ccnt=0;

    for($i=0;$i<count($all);$i++){

        if($all[$i]['ParentID']==0){
            $data[$pcnt]=$all[$i];

            for($j=0;$j<count($all);$j++){
                if($all[$j]['ParentID'] == $all[$i]['ServiceTypeID']){
                    $data[$pcnt][$ccnt]=$all[$j];
                    $ccnt++;        
                }
            }
            $ccnt=0;
            $pcnt++;
        }

    }
    echo json_encode($data2);
    exit;
}

2 个答案:

答案 0 :(得分:0)

以下是您的解决方案,使用此修改您的代码..

$data=array();
$all  = array(
        array('ServiceTypeID'=>1,'ParentID'=>'0','type'=>'abc'),
        array('ServiceTypeID'=>2,'ParentID'=>'0','type'=>'xyz'),
        array('ServiceTypeID'=>3,'ParentID'=>'2','type'=>'abs'),
        array('ServiceTypeID'=>6,'ParentID'=>'1','type'=>'bas'),
        array('ServiceTypeID'=>8,'ParentID'=>'2','type'=>'ddd')
    );

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

        foreach($all as $k => $v)
        { 
            if($val['ServiceTypeID'] == $v['ParentID']){
                $data[$key]['subBranches'][]= $v;
            }
        }
    }
}
echo "<pre>"; print_r($data);

这将输出:

Array
(
    [0] => Array
        (
            [ServiceTypeID] => 1
            [ParentID] => 0
            [type] => abc
            [subBranches] => Array
                (
                    [0] => Array
                        (
                            [ServiceTypeID] => 6
                            [ParentID] => 1
                            [type] => bas
                        )

                )

        )

    [1] => Array
        (
            [ServiceTypeID] => 2
            [ParentID] => 0
            [type] => xyz
            [subBranches] => Array
                (
                    [0] => Array
                        (
                            [ServiceTypeID] => 3
                            [ParentID] => 2
                            [type] => abs
                        )

                    [1] => Array
                        (
                            [ServiceTypeID] => 8
                            [ParentID] => 2
                            [type] => ddd
                        )

                )

        )

)

此处我只是print_r数据阵列,如果您需要,可以json_encode($data); ..

答案 1 :(得分:-2)

$array = array( array(), array(), ...);
$json = json_encode($array);
echo $json

编辑:

$array = array();
for ($i=0; $i < 5; $i++) { 
    array_push($array, array('iteration' => $i));
}
$json  = json_encode($array);
echo $json;

输出

  

[{&#34;迭代&#34;:0},{&#34;迭代&#34;:1},{&#34;迭代&#34;:2},{&#34;迭代&# 34;:3},{&#34;迭代&#34:4}]

[
    {
        "iteration":0
    },
    {
        "iteration":1
    },
    {
        "iteration":2
    },
    {
        "iteration":3
    },
    {
        "iteration":
    }
]

那不是你想要的吗?