我无法使用以下 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 关注
- 153 浏览
添加回答
举报
0/150
提交
取消