簡體   English   中英

檢查php中JSON對象數組中值的位置

[英]check the position of values in JSON object array in php

我有一個JSON(A行) ,如下所示。

   $fp = fopen('../feeds/ptp-ess_landing.json', 'w');
    fwrite($fp, json_encode($output));
    fclose($fp);
    logActivity();

    if(file_exists('../feeds/ptp-ess_landing.json')){
    $data = json_decode(file_get_contents('../feeds/ptp-ess_landing.json'));
    }

    {"toggle_multi_tiles":["0","1"]}  // Line A

我想要的是

=>上面A行的JSON對象數組中的第一個值, animation-delay:0s; 被申請;被應用。

=>第二個值, animation-delay:4s; 被申請;被應用。

這是我嘗試過的:

    <?php
    if($data->toggle_multi_tiles[0]) {                 /* For 1st element */
    ?>
    .multi-tiles a:nth-of-type(1) {
        animation-delay: 0s;
    }

    <?php } ?>

    <?php
    if($data->toggle_multi_tiles[1]) {              /* For 2nd element */
    ?>
    .multi-tiles a:nth-of-type(2) {
        animation-delay: 4s;
    }

    <?php } ?>

問題陳述:

我已經使用上面的if-logic 將0sec的動畫延遲應用於 JSON對象數組的第一個值,但是if條件將失敗,因為“ 0”為false。 我想知道我應該在上面的php代碼中進行哪些更改,以便

=>上面A行的JSON對象數組中的第一個值, animation-delay:0s; 被申請;被應用。

=>第二個值, animation-delay:4s; 被申請;被應用。

也許您應該遍歷整個數組。

foreach ($data->toggle_multi_tiles as $i => $value) {
?>
.multi-tiles a:nth-of-type(<?php echo $i+1; ?>) {
    animation-delay: <?php echo 4 * $value; ?>s;
}

因此,當數組元素為0您將獲得0s ;當數組元素為1時,您將獲得4s ;當數組元素為2時,您將獲得8s ,依此類推。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM