Page 1 of 1

Arrays within arrays

Posted: Wed May 13, 2009 5:25 pm
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!

Re: Arrays within arrays

Posted: Wed May 13, 2009 6:06 pm
by requinix
You can't put a while loop inside an array.

Look at the second example here.