i need some assistance with properly generating JSON. I almost have it, but I need to have each category and its related item in its own array. I have multiple categories each with many items under them. My JSON looks good, i just need the categories as an array along with its items. Any help is greatly appreciated!
Here is my code:
$channel ['items']['post']['categories'][$category_name]['details'][] = array(
'title' => $title,
'price' => $price,
'date_time' => $date_time,
'description' => $description,
);
}
$channels = array($channel);
$json = json_encode($channel);
header('Content-type: application/json');
echo $json;
My JSON looks like this currently:
{
"items": { /// THIS IS MEANT TO ORGANIZE UNDER ITEMS
"post": { ///THIS IS PROBABLY UNNECESSARY, I JUST HAVEN'T REMOVED IT.
"categories": {
"Baby": { ///I HAVE MANY MANY CATEGORIES
"details": [ ///I HAVE MANY MANY ITEMS UNDER EACH CATEGORY
{
"title": "trying with category id again",
"price": "3344.55",
"date_time": "2013-11-11 17:33:49",
"description": "Descriptor sb ",
"category_id": "3",
},
]
But I want it to look like this:
{
"items": {
"categories": [{ /// NEED THE BRACKET HERE
"Baby": {
"details": [
{
"title": "trying with category id again",
"price": "3344.55",
"date_time": "2013-11-11 17:33:49",
"description": "Descriptor sb ",
},
{
"title": "what the",
"price": "44444.66",
"date_time": "2013-11-18 20:15:58",
"description": "Blah blah",
},
]
},
"Baby Stuff": {
"details": [
{
"title": "putting in title",
"price": "3000.99",
"date_time": "2013-11-11 17:42:15",
"description": "Blah blah blah",
},
{
"title": "adding another listing",
"price": "400000.99",
"date_time": "2013-11-17 22:37:02",
"description": "Blah blah blah",
},
]
},
"More Baby Stuff": {
"details": [
{
"title": "does this work",
"price": "4000.77",
"date_time": "2013-11-18 19:59:49",
"description": "Description ",
},
{
categories
, but why would you than have a property in one of those elements namedBaby
rather than having that object have a property name likecategory name => 'baby'
. It would seem like an odd structure for accessing – Mike Brant Nov 26 '13 at 23:26items
CONTAIN MANY objects underneath it (in which case array would be applicable) or does it really contain a single item (in which case an object would be appropriate). Please add comments on your data structure. – Mike Brant Nov 26 '13 at 23:29