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;
}
Erstellen Sie die Objekte und Arrays in PHP-Code, dann verwenden Sie 'json_encode ($ top_obj);' – RiggsFolly
Zeigen Sie uns, was Sie bisher getan haben! – RiggsFolly
Und welche Ausgabe produziert das – RiggsFolly