php php打乱数组二维数组、多维数组
php中的shuffle函数只能打乱一维数组,有什么办法快速便捷的打乱多维数组?手册上提供了
|
上面这个是针对二维数组的!下面针对多维数组的乱序方法?尽可能的方便快速:
|
/**
* Shuffles an associative array recursive
*
* @param array $array
* @return array
*
*/
function
rec_assoc_shuffle(
$array
)
{
$ary_keys
=
array_keys
(
$array
);
$ary_values
=
array_values
(
$array
);
shuffle(
$ary_values
);
foreach
(
$ary_keys
as
$key
=>
$value
) {
if
(
is_array
(
$ary_values
[
$key
]) AND
$ary_values
[
$key
] != NULL) {
$ary_values
[
$key
] = rec_assoc_shuffle(
$ary_values
[
$key
]);
}
$new
[
$value
] =
$ary_values
[
$key
];
}
return
$new
;
- 上一篇 »PHP二维数组追加修改二维数组数据
- 下一篇 »php中count获取多维数组长度的方法