Here's a simple example of PHP scrambling a multidimensional array of array two-dimensional arrays. Content is very good, now share to everyone, but also for everyone to make a reference.
The shuffle function in PHP can only disrupt one-dimensional arrays, what are the ways to quickly and easily disrupt multidimensional arrays? The manual provides a
<?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 a two-dimensional array!
The following method for the chaos of multidimensional arrays? As convenient and quick as possible:
<?php function Shuffle_assoc ($list) {if (!is_array ($list)) return $list; $keys = Array_keys ($list); shuffle ($keys); $r Andom = 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 _v alues[$key] = rec_assoc_shuffle ($ary _values[$key]); } $new [$value] = $ary _values[$key]; } return $new;}
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!