0

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

3 Answers 3

2

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'];
Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

indeed something like that, but the primary categories in my case can be quite large and dynamic, as can the sub categories, with there listings per, and working that into a loop thats originating from a mysql query is whats got me beat at the moment.
Then I suspect you'll need to do as @Ninsuo suggests and manually check if that key exists, if not set it to an array, then populate the array.
0

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

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.