The shuffle function in php can only shuffle one-dimensional arrays. Is there any way to shuffle multi-dimensional arrays quickly and conveniently? 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!
The following shuffle method for multi-dimensional arrays? As convenient and fast 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 from the PHP manual, which can scramble multi-dimensional 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; }
The above is the PHP scrambled array two-dimensional array brought by the editor. This is all about the simple examples of multi-dimensional arrays. I hope everyone will support this site~
The above introduces a simple example of PHP multi-dimensional array, PHP scrambled array, two-dimensional array and multi-dimensional array, including the content of PHP multi-dimensional array. I hope it will be helpful to friends who are interested in PHP tutorials.