简体   繁体   English

PHP MySQL构建3层多维数组

[英]PHP MySQL building a 3 Tier multi dimensional array

So I have my query, its returning results as expect all is swell, except today my designer through in a wrench. 因此,我得到了我的查询,它的返回结果符合预期,但今天,除了我的设计师用扳手外,其他所有东西都在膨胀。 Which seems to be throwing me off my game a bit, maybe its cause Im to tired who knows, anyway.. 看来这似乎使我无法参加比赛,也许是因为无论如何我都会感到疲倦。

I am to create a 3 tier array 我要创建一个3层阵列

primary category, sub category (which can have multiples per primary), and the item list per sub category which could be 1 to 100 items. 主要类别,子类别(每个主要类别可以有多个),以及每个子类别的项目列表,可以是1到100个项目。

I've tried foreach, while, for loops. 我已经尝试过foreach,而对于循环。 All typically starting with $final = array(); 通常都以$final = array(); then the loop below that. 然后是下面的循环

trying to build arrays like: 试图建立像这样的数组:

$final[$row['primary]][$row['sub']][] = $row['item]
$final[$row['primary]][$row['sub']] = $row['item]

I've tried defining them each as there own array to use array_push() on. 我尝试将它们定义为各自的数组以使用array_push() And various other tactics and I am failing horribly. 以及其他各种战术,我都惨遭失败。 I need a fresh minded person to help me out here. 我需要一个有主见的人来帮助我。 From what type of loop would best suit my need to how I can construct my array(s) to build out according to plan. 从哪种类型的循环最适合我的需要,到如何构造阵列以按计划进行扩展。

The Desired outcome would be 期望的结果是

array(
   primary = array
             (
                sub = array
                      (
                        itemA,
                        itemB,
                        itemC
                      ),
                sub = array
                      (
                        itemA,
                        itemB,
                        itemC
                      ),
             ),
   primary = array
             (
                sub = array
                      (
                        itemA,
                        itemB,
                        itemC
                      ),
                sub = array
                      (
                        itemA,
                        itemB,
                        itemC
                      ),
             ),
)

Something like this.... 像这样...

$final = 
array(
    'Primary1'=>array(
        'Sub1'=>array("Item1", "Item2"),
        'Sub2'=>array("Item3", "Item4")
    ),
    'Primary2'=>array(
        'Sub3'=>array("Item5", "Item6"),
        'Sub4'=>array("Item7", "Item8")
    ),
);

You can do it using array_push but it's not that easy since you really want an associative array and array_push doesn't work well with keys. 您可以使用array_push做到这一点,但这并不是那么容易,因为您真的想要一个关联数组,而array_push不能很好地与键配合使用。 You could certainly use it to add items to your sub-elements 您当然可以使用它向子元素添加项目

array_push($final['Primary1']['Sub1'], "Some New Item");

Something like this during treatment of your request : 在处理您的请求期间类似这样的内容:

if (!array_key_exists($row['primary'], $final)) {
    $final[$row['primary']] = array();
}
if (!array_key_exists($row['sub'], $final[$row['primary']])) {
    $final[$row['primary']][$row['sub']] = array();
}
$final[$row['primary']][$row['sub']][] = $row['item'];

If I understand you correctly, you want to fetch a couple of db relations into an PHP Array. 如果我对您的理解正确,则希望将几个数据库关系获取到一个PHP数组中。

This is some example code how you can resolve that: 这是一些示例代码,您可以如何解决该问题:

<?php

$output = array();

$i = 0;
// DB Query
while($categories) { // $categories is an db result

    $output[$i] = $categories;

    $ii = 0;
    // DB Query
    while($subcategories) { // $subcategories is an db result

        $output[$i]['subcategories'][$ii] = $subcategories;

        $iii = 0;
        // DB Query
        while($items) { // $items is an db result

            $output[$i]['subcategories'][$ii]['items'][$iii] = $items;

            $iii++;
        }
        $ii++;
    }
    $i++;
}

print_r($output);

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM