-1

I am using a nested while loop to create a nested array that will look like this:

[276] => Array
    (
        [0] => 302
    )

[279] => Array
    (
        [0] => 290
        [1] => 291
        [1] => 223

But for some reason I am only getting one nested array showing up under 279 but I know the data I am querying is returning at least 12 records that should be under the 279 array.

Here is my while loop code:

while ($row = mysql_fetch_assoc($result)) {
    $term_id = $row[term_id];

    // Look up Children Categories
    $sql2 = "SELECT * FROM wp_term_taxonomy wpt where parent = $row[term_id]";

    // execute query:
    $result2 = mysql_query($sql2) or die('A error occured: ' . mysql_error());

    // fetch results:
    $count_2 = "0";
    while ($row2 = mysql_fetch_assoc($result2)) {
        $term_id2 = $row2['term_id'];
        $arr[$term_id] = array($count_2 => $row2[term_id]); 
        $count_2++;
    }

}

Can someone tell me what Im doing wrong? Its like the nested array resets each time so I just get one record under the nested array.

1

1 Answer 1

1

Your code:

$arr[$term_id] = array($count_2 => $row2[term_id]); 

This assigns a new array to $arr[$term_id]. It does not append to an existing array if you already put items there in a previous iteration of the loop, it overwrites it, or "resets it" as you put it. You told it to do that.

$arr[$term_id][$count_2] = $row2[term_id]; 

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.