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

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

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

Try this:

<?php

$kitty = array('kitten' => array());
$kitty['kitty'][] = array('kitty' => 'Tabby');
$kitty['kitty'][] = array('kitty' => 'Ruby');
$kitty['kitty'][] = array('kitty' => 'Silver');

var_dump($kitty);
var_dump(json_encode($kitty));

which results in: {"kitty":[{"kitty":"Tabby"},{"kitty":"Ruby"},{"kitty":"Silver"}]}

Tags:

Php

Json