Home Backend Development PHP Problem How to use php shuffle function

How to use php shuffle function

May 28, 2019 pm 01:11 PM

php shuffle function is used to rearrange the elements in the array in random order. The syntax is shuffle(array), and the parameter array is required and refers to the array to be used.

How to use php shuffle function

#How to use the php shuffle function?

Definition and usage

shuffle() function rearranges the elements in the array in random order.

This function assigns new key names to elements in the array, and existing key names will be deleted (see Example 1 below).

Syntax

shuffle(array)
Copy after login

Parameters

array Required. Specifies the array to use.

Return value: TRUE if successful, FALSE if failed.

PHP Version: 4

Change Log: As of PHP 4.2.0, the random number generator is automatically seeded.

Example 1

Rearrange the elements in the array in random order:

<?php
$my_array = array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"purple");
shuffle($my_array);
print_r($my_array);
?>
Copy after login

Output:

Array ( [0] => red [1] => blue [2] => green [3] => purple [4] => yellow )
Copy after login

Example 2

Rearrange the elements in the array in random order:

<?php
$my_array = array("red","green","blue","yellow","purple");
shuffle($my_array);
print_r($my_array);
?>
Copy after login

Output:

Array ( [0] => blue [1] => green [2] => yellow [3] => red [4] => purple )
Copy after login

The above is the detailed content of How to use php shuffle function. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)