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

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.

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

Thanks for replying.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.