Home > Backend Development > PHP Tutorial > thinkphp随机问题

thinkphp随机问题

WBOY
Release: 2016-06-23 13:49:12
Original
1366 people have browsed it

比如卖出10条新闻,10条新闻做了排序了:1、2、3、4、5、5、5、6、6、7;
当遇到相同排序号时显示随机显示。也就是相同排序号局部随机显示。
这个东西不知道怎么写了;能不能实现?


回复讨论(解决方案)

$arr = array( 
array('id' => 1,'name' => 'aaa','uid'=>1), 
array('id' => 2,'name' => 'bbb','uid'=>2), 
array('id' => 3,'name' => 'ccc','uid'=>3), 
array('id' => 4,'name' => 'ddd','uid'=>4), 
array('id' => 5,'name' => 'ccc','uid'=>4), 
array('id' => 6,'name' => 'bbb','uid'=>4), 
array('id' => 7,'name' => 'bbb','uid'=>5), 
array('id' => 8,'name' => 'fff','uid'=>6), 
array('id' => 9,'name' => 'ccc','uid'=>7), 
array('id' => 10,'name' => 'bbb','uid'=>8), 
array('id' => 11,'name' => 'ddd','uid'=>8), 
array('id' => 12,'name' => 'eee','uid'=>9), 
array('id' => 13,'name' => 'fff','uid'=>10), 
); 
每次刷新页面的时候,数组红色的部分随机排序,能不能实现?

$arr = array( array('id' => 1,'name' => 'aaa','uid'=>1), array('id' => 2,'name' => 'bbb','uid'=>2), array('id' => 3,'name' => 'ccc','uid'=>3), array('id' => 4,'name' => 'ddd','uid'=>4), array('id' => 5,'name' => 'ccc','uid'=>4), array('id' => 6,'name' => 'bbb','uid'=>4), array('id' => 7,'name' => 'bbb','uid'=>5), array('id' => 8,'name' => 'fff','uid'=>6), array('id' => 9,'name' => 'ccc','uid'=>7), array('id' => 10,'name' => 'bbb','uid'=>8), array('id' => 11,'name' => 'ddd','uid'=>8), array('id' => 12,'name' => 'eee','uid'=>9), array('id' => 13,'name' => 'fff','uid'=>10), ); usort($arr, function($a, $b) {  if($a['uid'] == $b['uid']) return rand(-1, 1);  return $a['uid'] > $b['uid'] ? 1 : -1;  });print_r($arr);
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