从数据库构建多维数组

时间:2017-04-05 05:56:55

标签: php arrays multidimensional-array

我试图将所有菜单项存储在数据库中作为一个数组(两个数组类型以递归方式合并)。这是我到目前为止所提出的:

// function to push associative array elements recursively
function array_push_assoc($array, $key, $value){
    $array[$key] = $value;
    return $array;
}

$menu_array = $dropdown = $single = array();

// getting menu items (sections)
$get_sections = Db::query("SELECT * FROM `sections` ORDER BY `place` ASC");

if($get_sections){
    foreach($get_sections as $menu_items){  
        $menu_item_id = $menu_items['key_id'];
        // getting sub-sections (in any)
        $get_children = Db::query("SELECT `id_lng`, `title`, `url`, `parent` FROM `sections` WHERE `lng`.`key_id` = `sections`.`id` AND `parent` = '$menu_item_id' AND `key_type` = '1' AND `published`= '1' ORDER BY `place` ASC ");
        if(mysqli_num_rows($get_children) > 0){
            foreach($get_children as $kids){
                // here I need to add all "kids" recursively so it will be like:
                // [Our Services] => Array
                //  (
                //      [Service One] => service-one.php
                //      [Service Two] => service-two.php
                //  )
                $dropdown = array($menu_items['title'] => array($kids['title'] => $kids['url']));
            }
        } else{
            // if there are no "kids" to form a dropdown menu - form an array of this type:
            //  [Single Menu Item 1] => singe-menu-item-1.php

            $single = array_push_assoc($single, $menu_items['title'], $menu_items['url']);
        }

我有两个问题:

1)我已经想出如何在循环中推送关联数组但不确定如何使用多维数组($dropdown

2)我知道如何合并两个数组,但我需要的是逐个连接一个循环 - 理想情况下我想得到这种类型的数组:

[Single Menu Item 1] => singe-menu-item-1.php
[Our Services] => Array
    (
        [Service One] => service-one.php
        [Service Two] => service-two.php
    )
[Single Menu Item 2] => singe-menu-item-2.php
[Single Menu Item 3] => singe-menu-item-3.php

[Contact us] => Array
    (
        [email us] => email.php
        [visit us] => visit.php
        [call us] => call.php
    )

1 个答案:

答案 0 :(得分:2)

由于它只有两个级别,我认为你可以使用push for child循环构建,例如:

<?php
$menu           =   array();
$get_sections   =   Db::query("SELECT * FROM `sections` ORDER BY `place` ASC");

if($get_sections){
    foreach($get_sections as $menu_items){
        $menu_item_id = $menu_items['key_id'];
        $get_children = Db::query("SELECT `id_lng`, `title`, `url`, `parent` FROM `sections` WHERE `lng`.`key_id` = `sections`.`id` AND `parent` = '$menu_item_id' AND `key_type` = '1' AND `published`= '1' ORDER BY `place` ASC ");
        if(mysqli_num_rows($get_children) > 0){
            # Create a base array
            $menu[$menu_items['title']] =   array();
            foreach($get_children as $kids){
                # Push current array with new sets of 
                $menu[$menu_items['title']][$kids['title']] =   $kids['url'];
            }
        } else{
            $menu[$menu_items['title']] =   $menu_items['url'];
        }
    }
}

print_r($menu);

我没有对此进行测试,我只是在脑海中进行测试......所以请记住这一点。