Thinkphp random problems

Source: Internet
Author: User
Thinkphp random problems such as selling 10 News, 10 News are sorted: 1, 2, 3, 4, 5, 5, 5, 6, 6, 7; when the same order number is encountered, it is displayed randomly. That is, the same order number is randomly displayed. I don't know how to write this thing. can I implement it? ------ Solution ---------------------- $ arr & nbsp; array (id & nbsp thinkphp random problem
For example, 10 News are sold, and 10 news are sorted: 1, 2, 3, 4, 5, 5, 6, 6, and 7;
When the same order number is encountered, it is displayed randomly. That is, the same order number is randomly displayed.
I don't know how to write this thing. can I implement it?
------ Solution ----------------------
$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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.