当前位置:网站首页>php 二维、多维 数组打乱顺序,PHP_php打乱数组二维数组多维数组的简单实例,php中的shuffle函数只能打乱一维
php 二维、多维 数组打乱顺序,PHP_php打乱数组二维数组多维数组的简单实例,php中的shuffle函数只能打乱一维
2022-07-02 09:41:00 【杭杭爸爸】
php打乱数组二维数组多维数组的简单实例
php中的shuffle函数只能打乱一维数组,有什么办法快速便捷的打乱多维数组?手册上提供了
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;
}
?>
上面这个是针对二维数组的!
下面针对多维数组的乱序方法?尽可能的方便快速:
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;
}
边栏推荐
- 电脑无缘无故黑屏,无法调节亮度。
- Mmrotate rotation target detection framework usage record
- MySQL stored procedure cursor traversal result set
- 解决uniapp列表快速滑动页面数据空白问题
- ESP32音频框架 ESP-ADF 添加按键外设流程代码跟踪
- Basic usage of MySQL in centos8
- MySql存储过程游标遍历结果集
- 制造业数字化转型和精益生产什么关系
- R HISTOGRAM EXAMPLE QUICK REFERENCE
- What are the methods of adding elements to arrays in JS
猜你喜欢
揭露数据不一致的利器 —— 实时核对系统
Verilog and VHDL signed and unsigned number correlation operations
webauthn——官方开发文档
Attribute acquisition method and operation notes of C # multidimensional array
tidb-dm报警DM_sync_process_exists_with_error排查
From the perspective of attack surface, see the practice of zero trust scheme of Xinchuang
Importerror: impossible d'importer le nom « graph» de « graphviz»
Jinshanyun - 2023 Summer Internship
How to Add P-Values onto Horizontal GGPLOTS
Develop scalable contracts based on hardhat and openzeppelin (II)
随机推荐
mysql链表数据存储查询排序问题
mysql 基本语句
How to Add P-Values onto Horizontal GGPLOTS
C file and folder operation
ESP32存储配网信息+LED显示配网状态+按键清除配网信息(附源码)
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
Some things configured from ros1 to ros2
ros缺少xacro的包
How to Create a Nice Box and Whisker Plot in R
JS -- take a number randomly from the array every call, and it cannot be the same as the last time
PYQT5+openCV项目实战:微循环仪图片、视频记录和人工对比软件(附源码)
flutter 问题总结
Always report errors when connecting to MySQL database
Installation of ROS gazebo related packages
From the perspective of attack surface, see the practice of zero trust scheme of Xinchuang
A sharp tool for exposing data inconsistencies -- a real-time verification system
Seriation in R: How to Optimally Order Objects in a Data Matrice
PLC-Recorder快速监控多个PLC位的技巧
念念不忘,必有回响 | 悬镜诚邀您参与OpenSCA用户有奖调研
八大排序汇总