Re: Thinking out loud - a continuation...

From: Date: Thu, 22 Mar 2012 11:17:15 +0000
Subject: Re: Thinking out loud - a continuation...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-317175@lists.php.net to get a copy of this message
On 2012-03-21, at 2:39 PM, Jay Blanchard <jay.blanchard@sigmaphinothing.org> wrote: > This is a continuation of the nested query thing I posted to the list a while back. I was > finally able to output a nested unordered array that worked out well, but scope-creep has come in > the door and I have to change gears. > > I have a project where I have multiple queries and each query uses the results from the > previous query to get it's results. I need to do one of two things, either out put a > multidimensional array that I can use json_encode() on or I have to format the output from the > queries as a JSON string. The resulting JSON will be used by a JavaScript widget and must be formed > correctly. I created the following array by hand: > > $userList = array("John" => array( > "email" => "john@demo.com", > "website" => "www.john.com", > "age" => "22", > "password" => "pass", > "description" => array( > "hair" => "blonde", > "eyes" => "blue", > "build" => "medium" > )), > "Anna" => array( > "email" => "anna@demo.com", > "website" => "www.anna.com", > "age" => "24", > "password" => "pass", > "description" => array( > "hair" => "brunette", > "eyes" => "hazel", > "build" => "petite" > ) > )); > > I ran it through json_encode() and got the following output > > > {"John":{"email":"john@demo.com","website":"www.john.com","age":"22","password":"pass","description":{"hair":"blonde","eyes":"blue","build":"medium"}},"Anna":{"email":"anna@demo.com","website":"www.anna.com","age":"24","password":"pass","description":{"hair":"brunette","eyes":"hazel","build":"petite"}}} > > jslint.com verifies this as good JSON (although I thought there had to be square brackets > around child arrays). > > If you were me would you just generate the JSON? If not what is he best way to output an array > that will nest properly for each subsequent query? > > Thanks for any insight! > > Would it not be easier to get the data from a view which has the tables joined? Then it would be one query and it's a simple matter to format the results into the multi dimensional array then json? Bastien

« previous php.general (#317175) next »