0

0

PHP中关联数组打乱并保留键名的实用教程

花韻仙語

花韻仙語

发布时间:2025-12-04 09:15:06

|

783人浏览过

|

来源于php中文网

原创

PHP中关联数组打乱并保留键名的实用教程

php内置的`shuffle()`函数在打乱数组时会重新分配数字键,导致关联数组的原始键名丢失。本文将深入探讨`shuffle()`的这一特性,并提供一个自定义函数`shuffle_assoc()`,通过巧妙地处理键和值,实现在打乱关联数组元素顺序的同时,完整保留其原有键名,确保数据结构的完整性和可访问性。

在PHP开发中,我们经常需要对数组进行随机排序操作。对于索引数组(即使用数字作为键的数组),直接使用shuffle()函数即可满足需求。然而,当处理关联数组(即使用字符串作为键的数组)时,shuffle()函数的行为可能会出乎意料,因为它会默认重新分配数字键,从而导致原始的命名键丢失。这在需要保持键值对完整性的场景下,会带来数据访问上的问题。

shuffle()函数对关联数组的影响

PHP官方文档明确指出,shuffle()函数会为数组中的元素分配新的键。这意味着它会移除所有现有的键,而不仅仅是重新排序它们。

考虑以下关联数组示例:

 "species/Amanita_aprica.html",
    "Amanita augusta" => "species/Amanita_augusta.html",
    "Amanita calyptratoides" => "species/Amanita_calyptratoides.html",
    "Amanita calyptroderma" => "species/Amanita_calyptroderma.html",
    "Amanita constricta" => "species/Amanita_constricta.html",
    "Amanita gemmata" => "species/Amanita_gemmata.html",
    "Amanita magniverrucata" => "species/Amanita_magniverrucata.html",
    "Amanita muscaria" => "species/Amanita_muscaria.html",
    "Amanita novinupta" => "species/Amanita_novinupta.html",
    "Amanita ocreata" => "species/Amanita_ocreata.html",
    "Amanita pachycolea" => "species/Amanita_pachycolea.html",
    "Amanita pantherina" => "species/Amanita_pantherina.html",
    "Amanita phalloides" => "species/Amanita_phalloides.html",
    "Amanita porphyria" => "species/Amanita_porphyria.html",
    "Amanita protecta" => "species/Amanita_protecta.html",
    "Amanita pruittii" => "species/Amanita_pruittii.html",
    "Amanita silvicola" => "species/Amanita_silvicola.html",
    "Amanita smithiana" => "species/Amanita_smithiana.html",
    "Amanita vaginata" => "species/Amanita_vaginata.html",
    "Amanita velosa" => "species/Amanita_velosa.html",
    "Amanita vernicoccora" => "species/Amanita_vernicoccora.html"
);

// 原始数组的部分内容
// print_r($speciesarray);

shuffle($speciesarray); // 尝试打乱数组
$speciesarray = array_slice($speciesarray, 0, 5); // 截取前5个元素

print_r($speciesarray);
echo "
"; reset($speciesarray); $choice = key($speciesarray); // 获取第一个元素的键 print_r($choice); ?>

运行上述代码,预期输出可能是类似 [Amanita silvicola] => species/Amanita_silvicola.html 这样的键值对,并且 $choice 变量能获取到 Amanita silvicola 这样的键名。然而,实际输出会是:

立即学习PHP免费学习笔记(深入)”;

Array ( [0] => species/Amanita_silvicola.html [1] => species/Amanita_gemmata.html [2] => species/Amanita_calyptratoides.html [3] => species/Amanita_vaginata.html [4] => species/Amanita_phalloides.html )
0

这清楚地表明,shuffle()操作将关联数组转换成了索引数组,所有原始的字符串键都被替换成了从0开始的数字键。

GPT Detector
GPT Detector

在线检查文本是否由GPT-3或ChatGPT生成

下载

解决方案:自定义shuffle_assoc()函数

为了在打乱关联数组的同时保留其键名,我们需要一个自定义的函数。核心思路是:

  1. 首先,获取数组的所有键。
  2. 然后,打乱这些键的顺序。
  3. 最后,根据打乱后的键顺序,重新构建一个新的数组,将原始的值与新顺序的键关联起来。

以下是实现这一功能的shuffle_assoc()函数:

 "species/Amanita_aprica.html",
    "Amanita augusta" => "species/Amanita_augusta.html",
    "Amanita calyptratoides" => "species/Amanita_calyptratoides.html",
    "Amanita calyptroderma" => "species/Amanita_calyptroderma.html",
    "Amanita constricta" => "species/Amanita_constricta.html",
    "Amanita gemmata" => "species/Amanita_gemmata.html",
    "Amanita magniverrucata" => "species/Amanita_magniverrucata.html",
    "Amanita muscaria" => "species/Amanita_muscaria.html",
    "Amanita novinupta" => "species/Amanita_novinupta.html",
    "Amanita ocreata" => "species/Amanita_ocreata.html",
    "Amanita pachycolea" => "species/Amanita_pachycolea.html",
    "Amanita pantherina" => "species/Amanita_pantherina.html",
    "Amanita phalloides" => "species/Amanita_phalloides.html",
    "Amanita porphyria" => "species/Amanita_porphyria.html",
    "Amanita protecta" => "species/Amanita_protecta.html",
    "Amanita pruittii" => "species/Amanita_pruittii.html",
    "Amanita silvicola" => "species/Amanita_silvicola.html",
    "Amanita smithiana" => "species/Amanita_smithiana.html",
    "Amanita vaginata" => "species/Amanita_vaginata.html",
    "Amanita velosa" => "species/Amanita_velosa.html",
    "Amanita vernicoccora" => "species/Amanita_vernicoccora.html"
);

shuffle_assoc($speciesarray); // 使用自定义函数打乱数组并保留键名
$speciesarray = array_slice($speciesarray, 0, 5); // 截取前5个元素

print_r($speciesarray);
echo "
"; reset($speciesarray); $choice = key($speciesarray); // 获取第一个元素的键 print_r($choice); ?>

使用shuffle_assoc()函数后,预期的输出将符合我们的需求:

Array ( [Amanita silvicola] => species/Amanita_silvicola.html [Amanita gemmata] => species/Amanita_gemmata.html [Amanita calyptratoides] => species/Amanita_calyptratoides.html [Amanita vaginata] => species/Amanita_vaginata.html [Amanita phalloides] => species/Amanita_phalloides.html )
Amanita silvicola

可以看到,数组的键名得到了完整的保留,并且 $choice 变量也正确地获取到了字符串键 Amanita silvicola。

注意事项与总结

  • 引用传递: shuffle_assoc()函数通过引用(&)接收数组参数。这意味着函数会直接修改传入的数组,而不是返回一个新的数组。这是PHP中修改变量的常见模式,需要注意其副作用。
  • 性能考量: 对于非常大的数组,此方法会先提取所有键,然后构建一个新数组。这会涉及额外的内存分配和迭代操作。在绝大多数应用场景下,这种开销是可接受的。如果数组规模达到数百万甚至更大,可能需要考虑更底层的优化或不同的数据结构。
  • 替代方案: 如果你只是想随机选择N个键值对,而不是对整个数组进行随机排序,array_rand()函数可能是一个更高效的选择。它允许你随机获取一个或多个键,然后你可以根据这些键来访问原始数组中的值。然而,array_rand()并不提供对整个数组进行随机排序的功能。

通过自定义shuffle_assoc()函数,我们有效地解决了PHP shuffle()函数在处理关联数组时丢失键名的问题。掌握这种技巧对于需要维护数据完整性的PHP开发者来说至关重要,它确保了在进行随机化操作时,关联数组的结构和语义得以保留。

相关专题

更多
php文件怎么打开
php文件怎么打开

打开php文件步骤:1、选择文本编辑器;2、在选择的文本编辑器中,创建一个新的文件,并将其保存为.php文件;3、在创建的PHP文件中,编写PHP代码;4、要在本地计算机上运行PHP文件,需要设置一个服务器环境;5、安装服务器环境后,需要将PHP文件放入服务器目录中;6、一旦将PHP文件放入服务器目录中,就可以通过浏览器来运行它。

2735

2023.09.01

php怎么取出数组的前几个元素
php怎么取出数组的前几个元素

取出php数组的前几个元素的方法有使用array_slice()函数、使用array_splice()函数、使用循环遍历、使用array_slice()函数和array_values()函数等。本专题为大家提供php数组相关的文章、下载、课程内容,供大家免费下载体验。

1669

2023.10.11

php反序列化失败怎么办
php反序列化失败怎么办

php反序列化失败的解决办法检查序列化数据。检查类定义、检查错误日志、更新PHP版本和应用安全措施等。本专题为大家提供php反序列化相关的文章、下载、课程内容,供大家免费下载体验。

1530

2023.10.11

php怎么连接mssql数据库
php怎么连接mssql数据库

连接方法:1、通过mssql_系列函数;2、通过sqlsrv_系列函数;3、通过odbc方式连接;4、通过PDO方式;5、通过COM方式连接。想了解php怎么连接mssql数据库的详细内容,可以访问下面的文章。

975

2023.10.23

php连接mssql数据库的方法
php连接mssql数据库的方法

php连接mssql数据库的方法有使用PHP的MSSQL扩展、使用PDO等。想了解更多php连接mssql数据库相关内容,可以阅读本专题下面的文章。

1444

2023.10.23

html怎么上传
html怎么上传

html通过使用HTML表单、JavaScript和PHP上传。更多关于html的问题详细请看本专题下面的文章。php中文网欢迎大家前来学习。

1235

2023.11.03

PHP出现乱码怎么解决
PHP出现乱码怎么解决

PHP出现乱码可以通过修改PHP文件头部的字符编码设置、检查PHP文件的编码格式、检查数据库连接设置和检查HTML页面的字符编码设置来解决。更多关于php乱码的问题详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1549

2023.11.09

php文件怎么在手机上打开
php文件怎么在手机上打开

php文件在手机上打开需要在手机上搭建一个能够运行php的服务器环境,并将php文件上传到服务器上。再在手机上的浏览器中输入服务器的IP地址或域名,加上php文件的路径,即可打开php文件并查看其内容。更多关于php相关问题,详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1307

2023.11.13

Java编译相关教程合集
Java编译相关教程合集

本专题整合了Java编译相关教程,阅读专题下面的文章了解更多详细内容。

9

2026.01.21

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP课程
PHP课程

共137课时 | 9万人学习

JavaScript ES5基础线上课程教学
JavaScript ES5基础线上课程教学

共6课时 | 9.1万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 0.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号