Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
高效的数据库查询(PHP+;PDO SQL)_Php_Multidimensional Array - Fatal编程技术网

高效的数据库查询(PHP+;PDO SQL)

高效的数据库查询(PHP+;PDO SQL),php,multidimensional-array,Php,Multidimensional Array,目前,我的数据库结构如下: | id | name | parent_id | 1 | Human Resources | 0 | 2 | Marketing | 0 | 3 | Operations | 0 | 4 | Design | 0 | 5 | Marketing Design| 4 | 6 | Graphic Design | 4 | 7 | Print Design | 4 | 8 | Human

目前,我的数据库结构如下:

| id | name            | parent_id
| 1  | Human Resources | 0
| 2  | Marketing       | 0
| 3  | Operations      | 0
| 4  | Design          | 0
| 5  | Marketing Design| 4
| 6  | Graphic Design  | 4
| 7  | Print Design    | 4
| 8  | Human Personal  | 1
| 9  | Food Ops        | 3
如您所见,这些是业务中的部门,也是子部门

子部门父部门id部门的id

例如:

id:4,名称:设计,父\u id:0

id:7,印刷设计,家长id:4

版画设计是设计的一个分支部门

我在一个查询中调用了数据库中的所有内容,现在我需要在以下结构中使用它们:

$depts = array(
   "Human Resources" => array("Human Personal"),
   "Marketing" => array(),
   "Operations" => array("Food Ops"),
   "Design" => array("Marketing Design", "Graphic Design", "Print Design"),
   ...
);
到目前为止,我已经:

 foreach ($results as $result) {

    if ($result['parent_id'] == 0) {
        $parentCategories_arr[array($result['id'] => $result['name'])];
    } else {
        $returnedResults_arr[$result['parent_id']] = array($result['name']);
    }
}
然而,我完全认为我没有抓住要点。因此,我的问题是:


我如何循环浏览结果的所有内容,并将父类别添加到一个数组中,并将其子类别作为一个数组?

也许有一个更简单的方法,但它是有效的:(讨厌说那句话)-试着做得更好

$mFinalArray = fixMyArray($results);

print_r($mFinalArray);

function fixMyArray($results){

    // Create categories - parent_id == 0
    foreach($results as $index => $result)                           // $index = 0|1|2|3|4|5|6|7|8|9
        if($result['parent_id'] == 0)                                // $result['parent_id'] = current item parent_id
            $mCategories[$result['name']] = $result['id'];           // $mCategories['Human Resources'] = 1|2|3|4

    // Insert each data to the right parent
    foreach($results as $index => $result)                           // $index = 0|1|2|3|4|5|6|7|8
        if($result['parent_id'] != 0)
            foreach($mCategories as $subindex => $category)          // $subindex = Human Resources | Marketing | Operations | Design
                if($result['parent_id'] == $category)                // $category = 0|1|2|3|4
                        $mFinalArray[$subindex][] = $result['name']; // ex. $mFinalArray['Human Resources'][] = Human Personal

    return $mFinalArray;    

}
*最后一行有一个额外的[]
$mFinalArray[$subindex]
[]
=$result['name']
。这意味着附加到数组

输出:

Array
(
    [Design] => Array
        (
            [0] => Marketing Design
            [1] => Graphic Design
            [2] => Print Design
        )

    [Human Resources] => Array
        (
            [0] => Human Personal
        )

    [Operations] => Array
        (
            [0] => Food Ops
        )

)

您可以使用获取父id并获取子类别的方法,您可以这样做