【发布时间】:2017-06-13 06:39:01
【问题描述】:
需要帮助找出我在这方面出了什么问题。
我从 PDO fetchAll(PDO::FETCH_ASSOC) 中得到这个结果
$sql->execute();
$result = $sql->fetchAll(PDO::FETCH_ASSOC);
得到这个
Array
(
[0] => Array
(
[father] => 1
[child] => 1
[timeOfchild] => 2
)
[1] => Array
(
[father] => 1
[child] => 1
[grandChild] => 2
)
[2] => Array
(
[father] => 1
[child] => 1
[grandChild] => 4
)
[3] => Array
(
[father] => 1
[child] => 1
[grandChild] => 3
)
[4] => Array
(
[father] => 1
[child] => 2
[grandChild] => 2
)
[5] => Array
(
[father] => 1
[child] => 2
[grandChild] => 3
)
[6] => Array
(
[father] => 1
[child] => 2
[grandChild] => 4
)
[7] => Array
(
[father] => 2
[child] => 1
[grandChild] => 4
)
[8] => Array
(
[father] => 2
[child] => 1
[grandChild] => 3
)
[9] => Array
(
[father] => 2
[child] => 1
[grandChild] => 2
)
[10] => Array
(
[father] => 2
[child] => 2
[grandChild] => 2
)
[11] => Array
(
[father] => 2
[child] => 2
[grandChild] => 3
)
[12] => Array
(
[father] => 2
[child] => 2
[grandChild] => 4
)
)
现在我使用 foreach 循环进行分组
$family = array();
// Loop JSON objects
foreach($result as $object) {
$father_key = $object['father'];
$child_key = $object['child'];
$grandChild_key = $object['grandChild'];
$children = 'children';
if(!array_key_exists($father_key, $family)) {
$fatherObject = array();
$fatherObject['title'] = 'Father' .$father_key;
$fatherObject['key'] = $father_key;
$fatherObject['children'] = array();
// Save this new object
$family[$father_key] = $fatherObject;
}
$week_children = $family[$father_key][$children];
if(!array_key_exists($child_key, $week_children)) {
$childObject = array();
$childObject['title'] = 'Child' .$child_key;
$childObject['key'] = $child_key;
$childObject['children'] = array();
// Save this new object
$family[$father_key]['children'][$child_key] = $childObject;
}
if(isset($family[$father_key][$children][$child_key][$children])){
$day_children = $family[$father_key][$children][$child_key][$children];
if(!array_key_exists($grandChild_key, $day_children)) {
$grandChildObject = array();
$grandChildObject['title'] = 'GrandChild' .$grandChild_key;
$grandChildObject['key'] = $grandChild_key;
// Save this new object
$family[$father_key][$children][$child_key]['children'][$grandChild_key] = $grandChildObject;
}
}
}
echo json_encode($family);
预期输出的形式应该是:
[
{"title": "Father 1", "key": "1", "children": [
{"title": "child 1", "key" : "1", "children" : [
{"title" : "grandchild 1" , "key" : "key 1" },
{"title" : "grandchild 2" , "key" : "key 2" }
]},
{"title": "child 2", "children" : [
{"title" : "grandchild 1" , "key" : "key 1" },
{"title" : "grandchild 2" , "key" : "key 2" }
]},
{"title": "child 3", "children" : [
{"title" : "grandchild 1" , "key" : "key 1" },
{"title" : "grandchild 2" , "key" : "key 2" }
]},
]},
{"title": "Father 2", "key": "1", "children": [
{"title": "child 1", "key" : "1", "children" : [
{"title" : "grandchild 1" , "key" : "key 1" },
{"title" : "grandchild 2" , "key" : "key 2" }
]},
{"title": "child 2", "children" : [
{"title" : "grandchild 1" , "key" : "key 1" },
{"title" : "grandchild 2" , "key" : "key 2" }
]},
{"title": "child 3", "children" : [
{"title" : "grandchild 1" , "key" : "key 1" },
{"title" : "grandchild 2" , "key" : "key 2" }
]},
]}
];
但我得到的是以 JSON_encode($family) 的形式作为 PHP 文件的输出。
{
"1":{
"title":"Father 1",
"key":"1",
"children":{
"1":{
"title":"Child 1",
"key":"1",
"children":{
"1":{
"title":"GrandChild 1",
"key":"1"
},
"2":{
"title":"GrandChild 2",
"key":"2"
}
}
},
"2":{
"title":"Child 2",
"key":"2",
"children":{
"1":{
"title":"GrandChild 1",
"key":"1"
},
"2":{
"title":"GrandChild 2",
"key":"2"
}
}
}
}
},
"2":{
"title":"Father 2",
"key":"2",
"children":{
"1":{
"title":"Child 2",
"key":"1",
"children":{
"1":{
"title":"GrandChild 1",
"key":"2"
},
"2":{
"title":"GrandChild 2",
"key":"3"
}
}
},
"2":{
"title":"Child 2",
"key":"2",
"children":{
"1":{
"title":"GrandChild 1",
................
};
【问题讨论】:
-
什么是'timeOfchild'?还有为什么他们没有自己的身份证?
-
它不应该在那里忘记删除它,这是另一个子组。