Home  >  Article  >  Backend Development  >  How to use php shuffle function

How to use php shuffle function

藏色散人
藏色散人Original
2019-05-28 13:11:213677browse

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)

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);
?>

Output:

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

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);
?>

Output:

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

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:
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