On Wed, Mar 21, 2012 at 2:39 PM, Jay Blanchard <jay.blanch...@sigmaphinothing.org> wrote: > ... > 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" => "j...@demo.com", > "website" => "www.john.com", > "age" => "22", > "password" => "pass", > "description" => array( > "hair" => "blonde", > "eyes" => "blue", > "build" => "medium" > )), > "Anna" => array( > "email" => "a...@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":"j...@demo.com","website":"www.john.com","age":"22","password":"pass","description":{"hair":"blonde","eyes":"blue","build":"medium"}},"Anna":{"email":"a...@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).
Speaking to your belief that arrays had to have square brackets, json_encode examines the PHP array and only encodes sequential numbers JSON arrays. Others (as in your case) are encoded as object literals: http://php.net/manual/en/function.json-encode.php That said, you can still access Javascript Object properties with array access if you prefer in the client code: http://www.quirksmode.org/js/associative.html > 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? Because of the options json_encode provides and the flexibility it affords while in PHP, I would generate PHP and then always use json_encode to generate the JSON as needed. Adam -- Nephtali: A simple, flexible, fast, and security-focused PHP framework http://nephtaliproject.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php