PHP函数shuffle()取数组若干个随机元素的方法分析,shuffle数组
PHP函数shuffle()取数组若干个随机元素的方法分析,shuffle数组
本文实例讲述了PHP函数shuffle()取数组若干个随机元素的方法。分享给大家供大家参考,具体如下:
有时候我们需要取数组中若干个随机元素(比如做随机推荐功能),那么PHP要如何实现呢?一个比较简单的解决方法是用PHP自带的shuffle()函数。下面举一个简单的例子:
$data[] = array( "name" => "帮客之家", "rank" => "40" ); $data[] = array( "name" => "博客园", "rank" => "50" ); $data[] = array( "name" => "CSDN", "rank" => "60" ); $data[] = array( "name" => "ITEYE", "rank" => "50" ); shuffle($data); $i = 0; foreach($data as $key =>$value ){ if($i < 2) { echo $data[$key]['name'].'<br />'; } $i++; }
shuffle()
shuffle() 函数把数组中的元素按随机顺序重新排列。若成功,则返回 TRUE,否则返回 FALSE。本函数为数组中的单元赋予新的键名,这将删除原有的键名而不仅是重新排序。
如果传入的是关联数组,你会发现关联数组的键名将丢失。顺便这里说一下解决方法:
// 打乱关联数组的排序 function shuffle_assoc($array) { $randomized_keys = array_rand($array, count($array)); foreach($randomized_keys as $current_key) { $output[$current_key] = $array[$current_key]; } return $output; }
另外,php还提供了从数组中随机抽取值的函数:array_rand(),其调用格式如下:array_rand(数组,抽取元素的个数); 当然要实现对二维数组的支持还需要封装一下才能使用。但前面的程序就很好支持二维数组。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
您可能感兴趣的文章:
- PHP中shuffle数组值随便排序函数用法
- JavaScript中实现PHP的打乱数组函数shuffle实例
- php数组函数序列 之shuffle()和array_rand() 随机函数使用介绍
- PHP数组函数array_multisort()用法实例分析
- PHP常见数组函数用法小结
- php数组函数array_key_exists()小结
- PHP中使用array函数新建一个数组
- php 利用array_slice函数获取随机数组或前几条数据
- php使用array_search函数实现数组查找的方法
- PHP数组和explode函数示例总结
- PHP使用in_array函数检查数组中是否存在某个值
- PHP数组相关函数汇总
- php使用gettimeofday函数返回当前时间并存放在关联数组里
- php访问数组最后一个元素的函数end()用法

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
