0

I want to merge json arrays with another json arrays which accessed from URL.

example1.json

"contents":[
            {
               "idSentence":[
                  118077
               ],
               "idParagraph":16424
            },
            {
               "idSentence":[
                  10402,
                  85562
               ],
               "idParagraph":16427
            },

]

For example, http://www.example.com/2016/04/news/idS/10402/f/json will returns:

"idSentence":10402,
    "sentence":"Compact but strong storms known as supercells reach Oklahoma, "

and http://www.example.com/2016/04/news/idS/85562/f/json will returns:

"idSentence":85562,
    "sentence":"which also brought many violent storms to the states of Kansas, Arkansas, and Tennessee."

My question is, how can I combine that jsons to shown like this with PHP & JS?

Id Paragraph     Id Sentence    Sentence
16427               10402       Compact but strong storms known as supercells reach Oklahoma, 
16427               85562       which also brought many violent storms to the states of Kansas, Arkansas, and Tennessee

There is a lot of sentence and paragraph, so I need an efficient way to do this. My idea is doing iterative process by retrieve the id sentence in every id paragraph, and call the json url of sentence that correspond to the id sentence, combine both of it, and show it to table

$jsonText = file_get_contents("example.json");
$jsonUrl = "http://www.example.com/2016/04/news/idS/idSentence/f/json"
$arr = json_decode($jsonText, true);

//This is the part of iterative process & call correspond id sentence

//This is the part of combining process and show it to table

I'm a relative beginner to json, so I'm still confused about how to solve this problem. Please help, thank you.

Arian Caen Holt
  • 119
  • 1
  • 1
  • 8

1 Answers1

1

When you decode the json, You'll have an array. Combine the array with array_merge_recursive and encode that array with json_encode, write inside the file.

Community
  • 1
  • 1
capcj
  • 1,535
  • 1
  • 16
  • 23
  • How about the id sentence that must be correspond? – Arian Caen Holt May 12 '17 at 11:29
  • The merge will do that for you, if you see the documentation, the function merge by indexes, resulting into an array with the exact order described as needed above. What specifically couldn't you understand? About the corresponding Id Paragraph? – capcj May 12 '17 at 11:35
  • Hi, I don't understand about the corresponding id paragraph. How to do it in JSON @calexandre? – Arian Caen Holt Jun 14 '17 at 03:41
  • Well, if you explain to me how do you get the id paragraph I can help you to figure out, the only routine showed was IdSentence/Sentence, with my answer you can get that layout you wanted, but to be honest with you the idParagraph initially i didn't get it when I made the answer, sry. – capcj Jun 21 '17 at 01:39