簡體   English   中英

從數據庫構建多維數組

[英]Building a multidimensional array from database

我試圖將所有菜單項作為數組存儲在數據庫中(兩個數組類型以遞歸方式合並到一個數組中)。 到目前為止,這是我想出的:

// 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
    )

由於只有兩個級別,我應該認為您可以使用push for child loop進行構建,例如:

<?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);

我還沒有測試過,我只是在腦海中瀏覽它...因此請記住這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM