PHP Shuffle function can only disturb the one-dimensional array, what is the way to quickly and easily disrupt the multidimensional array? The manual provides
<?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] = $list [$key];
return $random;
>
The above is for two-dimensional arrays!
What are the following random methods for multidimensional arrays? As quick and easy as possible:
<?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;
>
The following functions are also derived from the PHP manual, which can disrupt multidimensional arrays:
/**
* 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;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/