亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 PHP 中從 JSON 數組中獲取隨機項

在 PHP 中從 JSON 數組中獲取隨機項

PHP
慕森卡 2023-08-19 16:59:13
我無法使用以下 JSON 獲取隨機項目$str = file_get_contents("wisdomquotes.txt"); $array = json_decode($str, true); //Fine up to here$rand = $array[array_rand($array)];//Returns entire array instead of a single random item這是 JSON:{    "quotes": [{        "keywords": ["work"],        "quote": " A stich in time saves nine"    }, {        "keywords": ["health"],        "quote": " An apple a day keeps the doctor away."    }, {        "keywords": ["money"],        "quote": " A penny save is a penny earned."    }, {        "keywords": ["work"],        "quote": " You can't burn the candle at both ends."    }, {        "keywords": [""],        "quote": "Tis better to light a candle than to curse the darkness"    }]}獲得隨機物品的正確代碼是什么?
查看完整描述

3 回答

?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

這是因為您的情況下的主數組位于quotes子數組內。

$rand = $array['quotes'][\array_rand($array['quotes'])];


查看完整回答
反對 回復 2023-08-19
?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

你可以試試這個:


$str = file_get_contents("wisdomquotes.txt"); 

$array = json_decode($str, true);

$rand = array_rand($array['quotes'], 1);

var_dump($array['quotes'][$rand]);


查看完整回答
反對 回復 2023-08-19
?
LEATH

TA貢獻1936條經驗 獲得超7個贊

您可能會瞄準$array['quotes']

array_rand($array['quotes'], 1);


查看完整回答
反對 回復 2023-08-19
  • 3 回答
  • 0 關注
  • 201 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號