第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 foreach 循環(huán)中為 JSON 使用 array_key_exists

如何在 foreach 循環(huán)中為 JSON 使用 array_key_exists

PHP
Helenr 2022-07-09 09:50:34
我有一些代碼在我的項(xiàng)目中迭代 JSON 以在滑塊中顯示評(píng)論。此代碼有效,但是我只想顯示存在數(shù)組鍵“comment”的評(píng)論。我覺(jué)得解決方案必須使用array_key_exists,但我無(wú)法正確獲取代碼(我還是 PHP 新手)。我已經(jīng)嘗試在整個(gè) SO 上進(jìn)行搜索,但沒(méi)有取得多大成功。這是我正在使用的一些 JSON 的示例;REVIEW ID 1 是我要顯示的評(píng)論,而 REVIEW ID 2 是我要跳過(guò)的評(píng)論:{  "reviewId": "{REVIEW ID 1}",  "reviewer": {    "profilePhotoUrl": "{PROFILE PHOTO URL}",    "displayName": "PERSON 1"  },  "starRating": "FIVE",  "comment": "This is a review",  "createTime": "2019-08-30T15:38:59.412Z",  "updateTime": "2019-08-30T15:38:59.412Z",  "reviewReply": {    "comment": "{REVIEW REPLY}",    "updateTime": "2019-08-30T16:05:58.184Z"  },  "name": "accounts/{ACCOUNT NUMBER}/locations/{LOCATION NUMBER}/reviews/"},{  "reviewId": "{REVIEW ID 2}",  "reviewer": {    "profilePhotoUrl": "{PROFILE PHOTO URL}",    "displayName": "PERSON 2"  },  "starRating": "FIVE",  "createTime": "2019-02-07T14:59:28.729Z",  "updateTime": "2019-02-07T14:59:28.729Z",  "name": "accounts/{ACCOUNT NUMBER}/locations/{LOCATION NUMBER}/reviews/"},這是運(yùn)行評(píng)論的代碼:    $jsonreviews = plugin_dir_path( __DIR__ ) . './latest.json';    $reviews2var = file_get_contents($jsonreviews);    $reviews = json_decode($reviews2var, true);    $starpath = plugin_dir_url( __FILE__ ) . './img/fivestars.svg';    $truckpath = plugin_dir_url( __FILE__ ) . './img/chad_love_truck.png';    // Start buffer    ob_start();?>    <div class="reviews-background">     <div class="swiper-container review-container">        <div class="swiper-wrapper review-wrapper">         <?php            $counter = 1;             foreach ($reviews['reviews'] as $review) :                if ($counter > 3)  {                    //do nothing                } else {         ?>如何在上面正確實(shí)現(xiàn) array_key_exists,或者我應(yīng)該完全做其他事情?謝謝
查看完整描述

2 回答

?
長(zhǎng)風(fēng)秋雁

TA貢獻(xiàn)1757條經(jīng)驗(yàn) 獲得超7個(gè)贊

您可以檢查是否comment未在此處設(shè)置:


if ($counter > 3 || !isset($review['comment']))  {

    //do nothing

} else {

    $counter++;

    //HTML

}

但是,我可能會(huì)翻轉(zhuǎn)if邏輯,您不需要else:


if ($counter <= 3 && isset($review['comment']))  {

    $counter++;

    //HTML

}

如果您有大型數(shù)組,如果您顯示的數(shù)組超過(guò) 3 個(gè)(或某個(gè)數(shù)字),您可能希望跳出循環(huán):


if ($counter > 3)

    break;

} elseif (isset($review['comment']))  {

    $counter++;

    //HTML

}

如果你愿意,你可以用!array_key_existsandarray_key_exists代替s。isset


查看完整回答
反對(duì) 回復(fù) 2022-07-09
?
皈依舞

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊

我認(rèn)為這是簡(jiǎn)單的方法


if(isset($test[$key_check])){

    echo  $value = $test[$key_check];

}

或檢查數(shù)組是否存在:


if (array_key_exists($key_check, $test)) {

    return $test[$key_check];

}


查看完整回答
反對(duì) 回復(fù) 2022-07-09
  • 2 回答
  • 0 關(guān)注
  • 138 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)