2016-04-07 28 views
0

Ich möchte ein Array von Objekten Unteräste in die Hauptanordnung der gleichen ObjekteArray von Objekten innerhalb Array von Objekten

aus derselben Tabelle

Ich habe eine Tabelle mit dem Namen senden, die Haupt- und Unterzweige enthält zusammen ich brauche die Unterdaten in einem Array im Hauptobjekt hinzuzufügen und das Array sollte (Unterzweige)

Wie erhalte ich diese JSON genannt werden:

[ 
    { 
    "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 
     } 
    ] 
    } 
] 

, wie ich gleiche json mit PHP ich Benutzer diese Funktion erhalten json erhalten SMAE wie i

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; 
} 
+1

Erstellen Sie die Objekte und Arrays in PHP-Code, dann verwenden Sie 'json_encode ($ top_obj);' – RiggsFolly

+0

Zeigen Sie uns, was Sie bisher getan haben! – RiggsFolly

+0

Und welche Ausgabe produziert das – RiggsFolly

Antwort

0

ist die Lösung für Sie, Ihren Code mit dieser ändern ..

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

erhalten Sie folgende Ausgabe:

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 
         ) 

       ) 

     ) 

) 

Hier habe ich nur print_r Datenfeld können Sie, wenn Sie json_encode($data); brauchen ..

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

EDIT putit:

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

Ausgang ist

[{"Iteration": 0}, {"Iteration": 1}, {"Iteration": 2}, {"Iteration": 3}, {"Iteration": 4}]

oder

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

ist nicht das, was Sie wollen? Hier

+0

dies funktioniert nicht – PERSON

+0

@PERSON überprüfen, dass – nocalis

+0

Nein .. !! bitte chek die JSON in meiner Frage ich wnn Json wie Array von Objekten in Array von Objekten Dank .. – PERSON