1.11M Members

Adapted way of using PHP to output JSON assets

 
1
 

Hi everyone,

Let me first explain the idea:
I have a JSON document which has the following structure:

{
        "timeline":
        {
            "headline":"Example Content",
            "type":"default",
            "text":"Example Content",
            "startDate":"2013,1,1",
            "endDate":"2013,1,28",
            "date": [
                {
                    "startDate":"2013,1,10",
                    "headline":"Example Content",
                    "text":"Example Content",
                    "asset":
                    {
                        "media":"Example Content",
                        "credit":"",
                        "caption":""
                    }
                },
                {
                    "startDate":"2013,1,14",
                    "headline":"Example Content",
                    "text":"Example Content",
                    "asset":
                    {
                        "media":"Example Content",
                        "credit":"",
                        "caption":""
                    }
                },
                {
                    "startDate":"2013,1,21",
                    "headline":"Example Content",
                    "text":"Example Content",
                    "asset":
                    {
                        "media":"Example Content",
                        "credit":"",
                        "caption":""
                    }
                }
            ]
        }
    }

I want to be able to add the following sections using PHP that will get data from a database:

            {
                "startDate":"2013,1,14",
                "headline":"Example Content",
                "text":"Example Content",
                "asset":
                {
                    "media":"Example Content",
                    "credit":"",
                    "caption":""
                }
            },

Now people have been talking about json_encode() however, I am not quite sure 1) how to do this to achieve what I want and 2) whether that method would allow me to do what I want.

So basically could someone provide some help as to how to do it using json_encode() or if anyone knows of an alternative way, I would be happy to take all advice.

Thanks a bunch.
Jack

 
0
 

You will need to use json_decode to create an array of your JSON. Then you can add your new data to that array. Finally use json_encode to create the new JSON.

 
0
 

How do you do this, I have never worked with json_decode or json_encode.

Thanks for replying.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: