创建关联数组的3级子gouping

时间:2017-01-27 14:34:13

标签: php arrays json pdo

需要帮助弄清楚我在哪里出错了。

我从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",
                       ................
        };

2 个答案:

答案 0 :(得分:1)

我不完全理解你的剧本/意图,但我觉得你设置的时候 孩子们反对你想写的东西:

 // Save this new object
$family[$father_key]['children'][$child_key] = $childObject;

而不是

 // Save this new object
$family[$father_key]['children'][] = $childObject;

答案 1 :(得分:0)

我最终选择了该解决方案,因为我在使用JS时遇到了问题。

        $family = array();
        // Loop JSON objects

        foreach($result as $object) {

            $father_key = $object["father"];
            $child = $object["child"];
            $grandchild = $object["grandChild"];
            $children = "children";

            if (!multi_in_array($father, $family) ) {
                $fatherObject = array();
                $fatherObject["title"] = "father " .$father;
                $fatherObject["key"] = $father;
                $fatherObject["children"] = array();
                $family[] = $fatherObject;
            }
        }

        foreach($result as $object) {
            $father = $object["father"];
            $child = $object["child"];
            $grandchild = $object["grandChild"];
            $children = "children";

            foreach($family as $index => $value){           
                $myFather = $value["key"];
                $childrens = $value[$children];
                if($father === $myFather){
                    if (!multi_in_array($child, $childrens) ) {
                        $childObject = array();
                        $childObject["title"] = "child " .$child;
                        $childObject["key"] = $child;
                        $childObject["children"] = array();
                        // Save this new object
                        $family[$index]["children"][] = $childObject;
                    }
                }
            }
        }

        foreach($result as $object) {
            $father = $object["father"];
            $child = $object["child"];
            $grandchild = $object["grandChild"];
            $children = "children";

            foreach($family as $index => $value){
                $myFather = $value["key"];              
                $childrens = $value[$children];

                foreach($childrens as $childIndex => $childValues){
                    $myChild = $childValues["key"];
                    $child_childrens = $childValues[$children];

                    if($father === $myFather && $child === $myChild){
                        if (!multi_in_array($grandchild, $child_childrens) ) {
                            $grandChildObject = array();
                            $grandChildObject['title'] = 'grandChild ' .$grandchild;
                            $grandChildObject['key'] = $grandchild;
                            // Save this new object
                            $family[$index][$children][$childIndex]['children'][] = $grandChildObject;
                        }
                    }
                }
            }
        }

使用前面的解决方案,数组键不是以0开头,而是我会[1] => ......和[2] => ...当我的js代码试图解释数组时,最终产生错误。我试图使用array_values重新映射数组并让所有数组键从0开始,但这个函数不是递归的,只会执行第一级。因此,除非有人能指出如何重新映射最终数组以使所有数字键从零开始,否则我会这样做。