php 二维数组随机打乱的代码
//随机打乱二维数组顺序 方法public function shuffle_assoc($lists)
{
if(!is_array($lists)){
return $lists;
}else{
$keys = array_keys($lists);
//打乱数组的键排序
shuffle($keys);
$result = array();
foreach ($keys as $k=> $key){
$result[$k] = $list[$key];
}
}
return $result;
}
//测试方法中调用
public function demo(){
$data = array(
array("id"=>1,"title"=>"语文"),
array("id"=>2,"title"=>"数学"),
array("id"=>3,"title"=>"英语"),
array("id"=>4,"title"=>"物理"),
array("id"=>5,"title"=>"化学"),
array("id"=>6,"title"=>"生物"),
);
$result = $this->shuffle_assoc($data);
var_dump($result);
exit;
}
//输出结果
array(6) {
[0]=>
array(2) {
["id"]=>
int(5)
["title"]=>
string(6) "化学"
}
[1]=>
array(2) {
["id"]=>
int(2)
["title"]=>
string(6) "数学"
}
[2]=>
array(2) {
["id"]=>
int(3)
["title"]=>
string(6) "英语"
}
[3]=>
array(2) {
["id"]=>
int(4)
["title"]=>
string(6) "物理"
}
[4]=>
array(2) {
["id"]=>
int(1)
["title"]=>
string(6) "语文"
}
[5]=>
array(2) {
["id"]=>
int(6)
["title"]=>
string(6) "生物"
}
}