在html中反转foreach循环php中的json对象

时间:2017-05-19 20:36:28

标签: php json

我正在尝试反转一个数组,以便json对象的内容将以相反的方式呈现在前端。

我正在使用reverse_array()函数,例如此foreach(array_reverse($json) as $obj){....但是,当我使用此功能时,页面为空白。当我使用foreach($json as $obj){...时一切正常。

我的json对象看起来像这样

{
    "1.49514754373E+12": {
        "description": "This is the first post in the json obj but i want it to be second in my html",
        "fileNames": [
            "3.jpg"
        ],

    },

    "1.71284754373E+12": {
        "description": "This is the second post in the json obj but i want it to be first in my html",
        "fileNames": [
            "1.jpg"
        ],

    },

}

我的代码看起来像这样

  <?php
    $json = file_get_contents('auction.json');
    $json = json_decode($json);

    foreach(array_reverse($json) as $obj){

     if(isset($obj->description) && $obj->description != ''){

      echo "<div class='row auction-item'>";

        echo "<p>" .$obj->description . "</p>";

        for ($x=0; $x < count($obj->fileNames); $x++) {

          echo "<div class='col-1-3' style=\"background-image: url('" . "/images/" . $obj->fileNames[$x] . "');background-size: cover; background-repeat: no-repeat; background-position: center; height: 100%;\"></div>";

        }

      echo "</div>";
     }
    }
    ?>

2 个答案:

答案 0 :(得分:1)

json_decode()默认返回一个对象,而不是数组。

尝试json_decode($json, true);强制它返回一个数组:

$json = file_get_contents('auction.json');
$json = json_decode($json, true);

foreach(array_reverse($json) as $obj){
    // note that your nested objects are now also arrays, so you'll need
    // to change the way you read from them
    if(isset($obj['description']) && $obj['description'] != ''){
        echo "<div class='row auction-item'>";
        echo "<p>" . $obj['description'] . "</p>";

        for ($x=0; $x < count($obj['fileNames']); $x++) {
            echo "<div class='col-1-3' style=\"background-image: url('" . "/images/" . $obj['fileNames'][$x] . "');background-size: cover; background-repeat: no-repeat; background-position: center; height: 100%;\"></div>";
        }

        echo "</div>";
    }
}

答案 1 :(得分:1)

您正在伪造使用true into decode将结果作为数组并在此之后更改$ obj变量的调用。

<?php
    $json = file_get_contents('auction.json');
    $json = json_decode($json, true);

    foreach(array_reverse($json) as $obj){

     if(isset($obj['description']) && $obj['description'] != ''){

      echo "<div class='row auction-item'>";

        echo "<p>" .$obj['description'] . "</p>";

        for ($x=0; $x < count($obj['filenames']); $x++) {

          echo "<div class='col-1-3' style=\"background-image: url('" . "/images/" . $obj['filenames'][$x] . "');background-size: cover; background-repeat: no-repeat; background-position: center; height: 100%;\"></div>";

        }

      echo "</div>";
     }
    }
    ?>

提示:避免将转换函数放入循环调用中,我建议:

<?php
    $json = file_get_contents('auction.json');
    $json = array_reverse(json_decode($json, true));

    foreach($json as $obj){

     if(isset($obj['description']) && $obj['description'] != ''){

      echo "<div class='row auction-item'>";

        echo "<p>" .$obj['description'] . "</p>";

        for ($x=0; $x < count($obj['filenames']); $x++) {

          echo "<div class='col-1-3' style=\"background-image: url('" . "/images/" . $obj['filenames'][$x] . "');background-size: cover; background-repeat: no-repeat; background-position: center; height: 100%;\"></div>";

        }

      echo "</div>";
     }
    }
    ?>