javascript - 如何使用 PHP 和 JS 将 URL 中的 json 数组与另一个 json 数组组合起来

标签 javascript php html arrays json

我想将 json 数组与另一个从 URL 访问的 json 数组合并。

example1.json

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

]

例如, http://www.example.com/2016/04/news/idS/10402/f/json将返回:

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

http://www.example.com/2016/04/news/idS/85562/f/json将返回:

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

我的问题是,如何将 json 与 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

有很多句子和段落,所以我需要一种有效的方法来做到这一点。 我的想法是通过检索每个 id 段落中的 id 句子,并调用与 id 句子对应的句子的 json url,将两者结合起来,然后将其显示到表中进行迭代过程

$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

我是 json 的相对初学者,所以我仍然对如何解决这个问题感到困惑。 请帮忙,谢谢。

最佳答案

当你解码 json 时,你将得到一个数组。将数组与 array_merge_recursive 组合起来并使用 json_encode write inside the file 对该数组进行编码.

关于javascript - 如何使用 PHP 和 JS 将 URL 中的 json 数组与另一个 json 数组组合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43936389/

相关文章:

javascript - HTML5 使用 Canvas 旋转图像

javascript - 从服务器格式化此 JSON 数据时遇到问题

javascript - Gulp 4.browser-sync的问题//下面的任务没有完成 : browser-sync

php - Zend 验证 Db_NoRecordExists 和排除选项

html - 导航栏覆盖 Logo

html - 如何让 React Web 应用程序全屏显示

javascript - 很奇怪;当 $(this).width() 被调用时包含元素改变大小

php - 从智能卡读卡器读取数据

java - json post请求java不发送数据到服务器

javascript - Typeahead 输入元素样式