PHP: Give a name to an array of JSON objects?

json, php

Solution

You have to wrap your result in another array on the 'kitten' key :

$output = json_encode(['kitten' => $result]);

Problem

I have managed to get data from database in PHP file. From there(data.php), ``` $output = json_encode($result); ``` The result would be like this, ``` $output=[{"kitty":"Whitely"},{"kitty":"Tabby"},{"kitty":"Ruby"},{"kitty":"Silver"}] ``` So how do I give name "kitten" an array of kitty objects in php format? For example like ``` "kitten":[{"kitty":"Whitely"},{"kitty":"Tabby"},{"kitty":"Ruby"},{"kitty":"Silver"}] ```

Original source