array of objects inside array of objects

PERSON

I want to send an array of objects called subbranches into the main array of the same objects

From the same table

I have a table that contains Main and Sub branches together I need to add the sub data in an array in the main object and that array should be called (SubBranches)

How do I get This 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
      }
    ]
  }
]

how I get same json using php I user this function to get json smae like i 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;
}
Manjeet Barnala

Here is the solution for you, modify your code with this..

$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);

This Will Output:

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
                        )

                )

        )

)

Here i've just print_r data array, you can json_encode($data); if you need..

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related