php 二维数组打乱顺序
#===============排序函数================
function shuffle_assoc($list) {
if (!is_array($list)) return $list;
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key) {
$random[$key] = shuffle_assoc($list[$key]);
}
return $random;
}
#===============测试二维数组================
$data = array(
array("id"=>13,"name"=>"张三"),
array("id"=>1,"name"=>"李爱媛"),
array("id"=>6,"name"=>"蔡依林"),
array("id"=>8,"name"=>"陈思"),
array("id"=>21,"name"=>"何达玲"),
array("id"=>14,"name"=>"赵勇"),
);
$tmp = shuffle_assoc($data);
var_dump($tmp);
}
#===============打印结果================
array(6) {
[1]=> array(2) {
["id"]=> int(1)
["name"]=>string(9) "李爱媛"
}
[2]=>array(2) {
["name"]=>string(9) "蔡依林"
["id"]=> int(6)
}
[5]=>array(2) {
["id"]=>int(14)
["name"]=>string(6) "赵勇"
}
[4]=>array(2) {
["name"]=>string(9) "何达玲"
["id"]=>int(21)
}
[0]=>array(2) {
["id"]=>int(13)
["name"]=>string(6) "张三"
}
[3]=>array(2) {
["id"]=>int(8)
["name"]=>string(6) "陈思"
}
}