我無法使用以下 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'])];

慕妹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]);
- 3 回答
- 0 關注
- 201 瀏覽
添加回答
舉報
0/150
提交
取消