Home > Backend Development > PHP Tutorial > 数组随机取值。解决方案

数组随机取值。解决方案

WBOY
Release: 2016-06-13 10:21:15
Original
943 people have browsed it

数组随机取值。

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$json = album.' ,曲目:'.$row->truck.'<br>';}
Copy after login


有一个json格式的数组。储存唱片专辑及曲目。
如何随机取出4个唱片,每个唱片取出一首曲目,且这4首曲目名不重复?(album为唱片,truck为曲目。)

------解决方案--------------------
我说错了,不是 array_rand 会出现重复项
而是他原始的数据有重复,需要做去重处理
PHP code
$data = json_decode($json);foreach($data as $v) $r[$v->truck] = $v;$data = $r;$ar = array_rand($data, 4);foreach($ar as $k) {    echo '唱片:'.$data[$k]->album.' ,曲目:'.$data[$k]->truck.'<br>';}<div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template