【问题标题】:Creating a 3 level sub-gouping of an associative array创建关联数组的 3 级子组合
【发布时间】: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'?还有为什么他们没有自己的身份证?
  • 它不应该在那里忘记删除它,这是另一个子组。

标签: php arrays json pdo


【解决方案1】:

我最终选择了那个解决方案,因为我在使用带有 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 开始,但此函数不是递归的,只会执行第一级。因此,除非有人可以指出如何重新映射最终数组以使所有数字键从零开始,否则我将继续这样做。

【讨论】:

    【解决方案2】:

    我不完全理解您的脚本/意图,但我觉得当您设置 你想写的孩子对象:

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

    而不是

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-02
      • 2012-07-31
      • 2017-11-14
      • 2010-10-02
      • 2016-08-30
      • 2012-12-23
      • 1970-01-01
      • 2013-06-13
      相关资源
      最近更新 更多