我無法使用以下 JSON 獲取隨機(jī)項(xiàng)目$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" }]}獲得隨機(jī)物品的正確代碼是什么?
3 回答

神不在的星期二
TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊
這是因?yàn)槟那闆r下的主數(shù)組位于quotes
子數(shù)組內(nèi)。
$rand = $array['quotes'][\array_rand($array['quotes'])];

慕妹3146593
TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊
你可以試試這個(gè):
$str = file_get_contents("wisdomquotes.txt");
$array = json_decode($str, true);
$rand = array_rand($array['quotes'], 1);
var_dump($array['quotes'][$rand]);

LEATH
TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可能會(huì)瞄準(zhǔn)$array['quotes']
array_rand($array['quotes'], 1);
- 3 回答
- 0 關(guān)注
- 197 瀏覽
添加回答
舉報(bào)
0/150
提交
取消