我想将一个名为子分支的对象数组发送到相同对象的主数组中。
从同一张桌子上
我有一个包含主分支和子分支的表,我需要在主对象中的数组中添加子数据,这个数组应该被调用(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 i用户获得相同的json的,这个函数就像我的putit一样得到json smae。
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;
}发布于 2016-04-07 14:17:51
这里是您的解决方案,用以下代码修改代码。
$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);。
发布于 2016-04-07 12:13:09
$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;输出是
{“迭代”:0},{“迭代”:1},{“迭代”:2},{“迭代”:3},{“迭代”:4}
或
[
{
"iteration":0
},
{
"iteration":1
},
{
"iteration":2
},
{
"iteration":3
},
{
"iteration":
}
]这不是你想要的吗?
https://stackoverflow.com/questions/36475700
复制相似问题