使用 json:如何在 php 中随机显示 json 数组

我有这个代码json代码


[{"title":"Title 1","option":"Option 1"},{"title":"title 2","option":"Option 2"}]

我用它来显示它


foreach ($query->getResult() as $row) {

    foreach (json_decode($row->keywords) as $msg) {

        echo $msg->title . "<br/>" . $msg->option . "<hr/>";

      }

}

我怎样才能随机显示这个?


皈依舞
浏览 79回答 1
1回答

德玛西亚99

您可以提取关键字数组,然后在显示前对其进行打乱:foreach ($query->getResult() as $row) {&nbsp; &nbsp; $keywords = json_decode($row->keywords);&nbsp; &nbsp; shuffle($keywords);&nbsp; &nbsp; foreach ($keywords as $msg) {&nbsp; &nbsp; &nbsp; &nbsp; echo $msg->title . "<br/>" . $msg->option . "<hr/>";&nbsp; &nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP