简体   繁体   English

在HTML中的foreach循环php中反转一个json对象

[英]reverse a json object in foreach loop php in the html

I am trying to reverse an array so that the content of a json object will be rendered on the front end in reverse. 我正在尝试反转数组,以便将json对象的内容反转显示在前端。

I am using the reverse_array() function, like this foreach(array_reverse($json) as $obj){.... however, when I use this the page is blank. 我正在使用reverse_array()函数,例如foreach(array_reverse($json) as $obj){....但是,当我使用此页面时,该页面为空白。 when I use foreach($json as $obj){... everything works. 当我使用foreach($json as $obj){...一切正常。

My json object looks like this 我的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"
        ],

    },

}

My code looks like this 我的代码看起来像这样

  <?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>";
     }
    }
    ?>

json_decode() returns an object by default, not an array. json_decode()默认返回一个对象,而不是数组。

Try json_decode($json, true); 尝试json_decode($json, true); to force it to return an array: 强制它返回一个数组:

$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>";
    }
}

You were forgeting to use true into decode to bring result as array and change the call of the $obj variable after that. 您忘记了使用true进行解码以将结果作为数组带来,并在此之后更改$ 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>";
     }
    }
    ?>

TIP: Avoid put conversion functions into loop calls, I suggest: 提示:避免将转换函数放入循环调用中,我建议:

<?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>";
     }
    }
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM