【问题标题】:How to combine a json array with another json arrays from URL with PHP & JS如何使用 PHP 和 JS 将 json 数组与来自 URL 的另一个 json 数组结合起来
【发布时间】:2017-10-11 16:39:52
【问题描述】:

我想将 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的相对初学者,所以我仍然对如何解决这个问题感到困惑。 请帮忙,谢谢。

【问题讨论】:

    标签: javascript php html arrays json


    【解决方案1】:

    当您解码 json 时,您将拥有一个数组。将数组与array_merge_recursive 组合并使用json_encode 对该数组进行编码,write inside the file。

    【讨论】:

    • 必须对应的id句怎么样?
    • 合并将为您执行此操作,如果您查看文档,则函数按索引合并,从而生成具有上述所需确切顺序的数组。具体有什么不明白的?关于对应的Id段落?
    • 嗨,我不明白对应的id段落。如何在 JSON @calexandre 中做到这一点?
    • 好吧,如果您向我解释如何获得 id 段落,我可以帮助您弄清楚,唯一显示的例程是 IdSentence/Sentence,通过我的回答,您可以获得您想要的布局,但老实说,idParagraph 最初我在回答时没有得到它,对不起。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多