【问题标题】:PHP: Reverse a JSON arrayPHP:反转 JSON 数组
【发布时间】:2016-01-18 09:16:00
【问题描述】:

有人可以帮我一些PHP吗?

原始代码~工作,但输出顺序错误。所以我需要反转 JSON 数组的顺序/顺序。

<?php

$url = "https://api.typeform.com/XXX";

$json = file_get_contents($url);

$data = json_decode($json, true);

foreach ($data["responses"] as $item) {

    if ($item["answers"]["textfield_14052468"] != '') {

        $intitule = $item["answers"]["textfield_14052468"];
        $description = $item["answers"]["textarea_14052495"];
        $boite = $item["answers"]["textfield_14051470"];
        $contact = $item["answers"]["textfield_14053600"];
        $type = $item["answers"]["list_15488974_other"];
        $contactmail = $item["answers"]["email_14053555"];
        $madate = $item["answers"]["date_14052792"];
        $file = $item["answers"]["fileupload_14052536"];
        $contract = $item["answers"]["textarea_14052561"];
        $web = $item["answers"]["website_14052525"];
        $date_submit = $item["metadata"]["date_submit"];

我可以找到 array_reverse

【问题讨论】:

  • 请注意,按照设计,JSON 没有顺序。这意味着它可以更改,恕不另行通知!

标签: php json array-reverse


【解决方案1】:
$json = file_get_contents($url,0,null,null); 
$tmp = json_decode($json, true);    // using a temp variable for testing
$result = $tmp;
$result['data'] = array_reverse($result['data']);

foreach ($result['data'] as $event) {
    echo '<div>'.$event['name'].'</div>'; //in order to display in div

【讨论】:

  • 将 $result['data'] 设为 $data["responses"]
【解决方案2】:
$url = 'http://link.to.api'
$json = file_get_contents($url,0,null,null); 
$tmp = json_decode($json, true);

$result = array_reverse($tmp);

print "<pre>";
print_r($result);
print "</pre>";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2016-08-01
    相关资源
    最近更新 更多