How to use php shuffle function

藏色散人
Release: 2023-02-22 20:50:01
Original
3734 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template