Rumah > Artikel > pembangunan bahagian belakang > thinkphp随机问题
比如卖出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);