Arrays within arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joebob2
Forum Newbie
Posts: 2
Joined: Wed Feb 25, 2009 8:36 pm

Arrays within arrays

Post by joebob2 »

Hey guys,

I'm trying to build an array of arrays from data pulled from my MySQL database.
After that, I need to use the json_encode function on it so I can pass it to a JavaScript script.

I have code below that I've been scratching my head over for days.
It seems like it should be simple enough, but I can't figure out how to properly
generate this. I keep getting an "Unexpected T_WHILE" error where the while loop
is below.

I can get it to work if it's all static, but I can't figure out how to get it to work while
drawing a variable number of rows from the database.

Code: Select all

// The script needs to return an array of arrays encoded in JSON format
$resource = mysql_query("-- insert query here");
 
echo json_encode(
    array(
        while($result = mysql_fetch_array($resource))
            {
 
                array(
                    'foo'    => $result["foo"],
                    'bar'    => $result["bar"],
                    'fooey'    => $result["fooey"],
                    );
    
            }    
        ));
Thanks in advance for any help at all!
Last edited by Benjamin on Wed May 13, 2009 5:31 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Arrays within arrays

Post by requinix »

You can't put a while loop inside an array.

Look at the second example here.
Post Reply