简体   繁体   中英

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.

I am using the reverse_array() function, like this foreach(array_reverse($json) as $obj){.... however, when I use this the page is blank. when I use foreach($json as $obj){... everything works.

My json object looks like this

{
    "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.

Try 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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