简体   繁体   中英

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

primary category, sub category (which can have multiples per primary), and the item list per sub category which could be 1 to 100 items.

I've tried foreach, while, for loops. All typically starting with $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. 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. 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.

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

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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