mreynol
mreynol

Reputation: 409

PHP JSON add array

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 ",
                    },
                    {

Upvotes: 1

Views: 310

Answers (2)

Mike Brant
Mike Brant

Reputation: 71384

Based on comments and edits to OP, perhaps you should be looking at a data structure like this:

{
  "categories":
    [
      {
        "category_name": "Baby",
        ... [Other category metadata],
        "category_items":
           [
             {
               "item_name": "Item Name",
               ... [Other item information]
             },
             { [next item] },
             ...
           ]
      },
      { [ next category ] },
      ...
   ]
}

This means one might build this object in similar manner to the following (assuming for example you are building this from DB query):

$channel = new stdClass();
$channel->categories = array();

LOOP for categories as $category
    $category = new stdClass();
    $category->category_name = 'some value';
    $category->some_other_category_metadata = 'some other value';
    $category->category_items = array();
    INNER LOOP on items in category as $item
        $item = new stdClass();
        $item->item_name = 'item name';
        $item->some_other_item_data = 'data';
        $category->category_items[] = $item;
    END INNER LOOP
    $channel->categories[] = $category;
END OUTER LOOP

json_encode($channel);

Upvotes: 1

maček
maček

Reputation: 77778

Add another [] after ['categories'] and that should do the trick

$channel ['items']['post']['categories'][][$category_name]['details'][] = array( // ...

$channel ['items']['post']['categories'][]['Baby']['details'][] = array(
    'title' => 'trying with category id again',
    'price' => 3344.55,
    'date_time' => '2013-11-11 17:33:49',
    'description' => 'Descriptor sb',

   );

$channels = array($channel);
$json = json_encode($channel);
echo $json;

Output

{
    "items": {
        "post": {
            "categories": [
                {
                    "Baby": {
                        "details": [
                            {
                                "title": "trying with category id again",
                                "price": 3344.55,
                                "date_time": "2013-11-11 17:33:49",
                                "description": "Descriptor sb"
                            }
                        ]
                    }
                }
            ]
        }
    }
}

See it working here on ideone

Upvotes: 2

Related Questions