PHP中的shuffle函数只能打乱一维数组,有什么办法快速便捷的打乱多维数组?手册上提供了
PHP;">
上面这个是针对二维数组的!
PHP;">
$keys = array_keys($list);
shuffle($keys);
$random = array();
foreach ($keys as $key)
$random[$key] = shuffle_assoc($list[$key]);
shuffle($keys);
$random = array();
foreach ($keys as $key)
$random[$key] = shuffle_assoc($list[$key]);
return $random;
}
?>
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;
}
{
$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;
}