Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

shuffle()

Example

Rearrange the elements in the array in a random order.

<?php
$my_array = array(“red”,“green”,“blue”,“yellow”,“purple”);

shuffle($my_array);
print_r($my_array);
?>

Definition and Usage

The shuffle() function randomizes the order of elements in the array.

 

This function assigns new keys to the elements, removing any existing keys (see example below).

Syntax

shuffle(array)

Parameter Values

 

Parameter

Description

array

Required. Specifies the array to be used.

Technical Details

Return Value:

Returns TRUE on success or FALSE on failure.

PHP Version:

4+

PHP Changelog:

 PHP 4.2: The random number generator is automatically seeded.

More Examples

Example

Shuffle the elements in the array into a random order.

<?php
$my_array = array(“a”=>“red”,“b”=>“green”,“c”=>“blue”,“d”=>“yellow”,“e”=>“purple”);

shuffle($my_array);
print_r($my_array);
?>