Home > Backend Development > PHP Tutorial > PHP shuffles the array, usage of shuffle function and simple example_php example

PHP shuffles the array, usage of shuffle function and simple example_php example

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-06 13:32:14
Original
944 people have browsed it

shuffle()

PHP shuffle() function randomly arranges the order of array cells (shuffles the array). This function assigns new keys to the elements in the array. This will delete the original keys rather than just reorder them.

Syntax:

bool shuffle (array &array)

Example 1:

<&#63;php
$arr = range(1,8);
print_r($arr);
echo '<br />';
shuffle($arr);
print_r($arr);
&#63;>
Copy after login

Output of running this example:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 ) 
Array ( [0] => 6 [1] => 1 [2] => 3 [3] => 2 [4] => 5 [5] => 7 [6] => 8 [7] => 4 ) 
Copy after login

It should be noted that every time the page is refreshed, the print_r($arr) result after shuffle($arr) is different. Since PHP 4.2.0, there is no longer a need to use functions such as srand() to seed the random number generator and the system will do it automatically.

Example 2, using associative array:

<&#63;php
$arr = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
shuffle($arr);
print_r($arr);
&#63;>
Copy after login

Run this example output:

Array ( [0] => 5 [1] => 2 [2] => 1 [3] => 3 [4] => 4 )

Of course, the output results will be different every time you refresh the page.

The above is the usage and simple examples of shuffle function in PHP that the editor brings to you. I hope you will support Script Home~

Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template