1

I am trying to create a nested array, in order to produce a nested list within a webpage.

So far I have managed to get the following:

    Array
(
[2012] => Array
    (
        [Show 1] => Array
            (
                [0] => Class 1
            )

        [Show 2] => Array
            (
                [0] => Class 1
            )

    )

[2009] => Array
    (
        [Show 1] => Array
            (
                [0] => Class 1
            )

    )

[2008] => Array
    (
        [Show 1] => Array
            (
                [0] => Class 1
            )

    )

)

However my actual results have more than 1 class per show, so it should look like:

[2012] => Array
    (
        [Show 1] => Array
            (
                [0] => Class 1
                [1] => Class 2
                [2] => Class 3
            )
etc etc etc.

I've managed this far, but haven't a clue how to continue, in order to get more than one class per show.

My code is as follows:

$handlerresults = $db->query("SELECT SHOW_NAME, YEAR, CLASS_NAME FROM vwhandlerresults WHERE HANDLER_ID = $gethandlerid ORDER BY YEAR DESC");
$showname = '';
while($row = $handlerresults->fetch_array(MYSQLI_ASSOC)) {
$year = $row['YEAR'];
$show = $row['SHOW_NAME'];
$results[$year][$show] = array($row['CLASS_NAME']);
}
print_r($results);

1 Answer 1

1

Try changing

$results[$year][$show] = array($row['CLASS_NAME']);

to

$results[$year][$show][] = $row['CLASS_NAME']

I think the problem is that you're overwriting your array each time you pass through the loop with the same 'show'. If you do what I said above, instead of overwriting, it'll put the next class into the next available index.

Sign up to request clarification or add additional context in comments.

5 Comments

Perfect! Now I've just got to figure out how to get this into a nested UL list.
Good luck! That should be a bit easier, but feel free to come back and ask if you have trouble :)
Can I ask about the unordered list here, or should I open a new thread?
You should probably open a new thread seeing as it's sort of a different topic. Post the link here if you like and I'll take a look.

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.