1,105,391 Community Members

Adapted way of using PHP to output JSON assets

Member Avatar
scaasiboi
Junior Poster in Training
51 posts since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
pritaeas
mod_pritaeas
11,316 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
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.

Member Avatar
scaasiboi
Junior Poster in Training
51 posts since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Thanks for replying.

Member Avatar
AleMonteiro
Master Poster
758 posts since Aug 2010
Reputation Points: 115 [?]
Q&As Helped to Solve: 143 [?]
Skill Endorsements: 29 [?]
 
0
 
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: