php打乱数组二维数组多维数组的简单实例
php中的shuffle函数只能打乱一维数组,有什么办法快速便捷的打乱多维数组?手册上提供了
<?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; } ?>
上面这个是针对二维数组的!
下面针对多维数组的乱序方法?尽可能的方便快速:
<?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; } ?>
以下函数也是出自php手册,可以打乱多维数组:
/** * 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; }
相关推荐
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
yifangs 2020-10-13
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28
wwwsurfphpseocom 2020-10-28
diskingchuan 2020-10-23
savorTheFlavor 2020-10-23