Looking to output json in the following format:
[{"Montgomery":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}],"Suburban":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}],"Shady Grove Adventist":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}],"Holy Cross":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}],"Washington Adventist":[{"red":"12:34:56","yellow":"11:44:46","orange":"10:54:36","green":"9:24:26","purple":"8:14:16"}]}]
My code:
$xyz[] = array("Montgomery"=> array("Red" => "12:00", "Yellow" => "14:00"));
$xyz[] = array("Suburban"=> array("Yellow" => "16:00"));
echo '[' . json_encode($xyz) . ']';
My results:
[[{"Montgomery":{"Red":"12:00","Yellow":"14:00"}},{"Suburban":{"Yellow":"16:00"}}]]
3 Answers 3
This will give you the structure:
$container = array();
$container['Montgomery'] = array(array('red' => '12:34:56', 'yellow' => '11:44:46'));
$container['Suburban'] = array(array('red' => '12:34:56', 'yellow' => '11:44:46'));
echo json_encode(array($container));
1 Comment
You could use objects, something like this (you'll want to clean it up a bit):
class Church {
public $red = "";
public $yellow = "";
public $orange = "";
public $green = "";
public $purple = "";
}
class XYZ {
public $Montgomery = new Church();
public $Shad_Grove_Adventist = new Church();
public $Holy_Cross = new Church();
public $Washington_Adventist = new Church();
}
$xyz = new XYZ();
$xyz->Montgomery->red = "12:00";
...
then output your JSON:
echo '[' . json_encode($xyz) . ']';
It won't be a perfect match to your desired JSON output, but it will give better readability and much more flexibility.
2 Comments
public $Shad_Grove Adventist ? ;)You're desired output has an array [] as its top level item. [] in JSON corresponds to a numeric array in PHP. A JSON array is used to contain an ordered collection of items.
Your top-level array contains one item, a JSON object {}. A PHP object (stdClass) or associative array will convert to this in JSON. A JSON object is used to create collections of key-value pairs.
To produce your desired output, build your data in PHP like this:
// Top-level numeric array.
$container = array();
// Only item in top-level array.
$item = array();
// The value of each of these items will be a numeric array.
$item['Montgomery'] = array();
// Create a new associative array, and push that onto the list array.
$subItem = array("red" => "12:34:56",
"yellow" => "11:44:46",
"orange" => "10:54:36",
"green" => "9:24:26",
"purple" => "8:14:16");
$item['Montgomery'][] = $subItem;
$container[] = $item;
// ...Add more items...
print json_encode($container);
Or, if you want to build it all at once:
$container = array(
array(
"Montgomery" => array(
array("red" => "12:34:56",
"yellow" => "11:44:46",
"orange" => "10:54:36",
"green" => "9:24:26",
"purple" => "8:14:16"
)
)
// ...More items...
)
);
print json_encode($container);
Notice that there are places that have an array inside an array. This is to build an associative array and add it as the only member of a numeric array. This corresponds to having a {} inside [].