【发布时间】:2020-07-23 14:33:35
【问题描述】:
我在使用以下 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"
}]
}
获取随机物品的正确代码是什么?
【问题讨论】: