簡體   English   中英

在HTML中的foreach循環php中反轉一個json對象

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

我正在嘗試反轉數組,以便將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>";
     }
    }
    ?>

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

您忘記了使用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>";
     }
    }
    ?>

提示:避免將轉換函數放入循環調用中,我建議:

<?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