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"}]
Array Datatype in JSON Similar to other programming languages, a JSON Array is a list of items surrounded in square brackets ([]). Each item in the array is separated by a comma. The array index begins with 0. The square brackets [...] are used to declare JSON array.
Yes, you can create an array of objects. Technically, a 2D array is valid JSON.
JSON data structures are very similar to PHP arrays. PHP has built-in functions to encode and decode JSON data. These functions are json_encode() and json_decode() , respectively. Both functions only works with UTF-8 encoded string data.
A JSON array is produced by assigning a contiguous integer-indexed PHP array into a JSON domain tree path. A PHP array variable is contiguous integer-indexed if it contains only integer keys that are sequenced from 0 to n -1 (where n is the total number of items in the array).
You have to wrap your result in another array on the 'kitten' key :
$output = json_encode(['kitten' => $result]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With