Site Search
Homepage of Otaku No Zoku
Complete Archives of Otaku No Zoku
About Otaku No Zoku
Subscribe to Otaku No Zoku
Bookmark Otaku No Zoku

PHP array_rand() Function :

Unlike most languages, when you need to extract a few random elements from an array, PHP provides built-in functionality. The PHP array_rand() function will return either a random index or key, or an array of random indices or keys, given an input of an array and the number of indices or keys to return.

PHP array_rand() Function Definition

mixed array_rand(array $source_array[, int $quantity = 1])

source_array

Required

The source array to use for extracting the indices or keys from.

quantity

Optional

Default value of 1. Specifies how many random keys or indices to return.

How this works is best illustrated with an example:

Extracting Random Elements From An Array

This example shows how to extract random elements from an already existing array.

Steps

  1. Allocate a small array of text strings.
  2. Use the array_rand() function to return a number of random indices or random keys.
  3. Print the randomly selected keys and the elements of the source array that match those keys.

The Source Code

<?php
// allocate a small array of strings
$names = array("JUSTIN", "CATHERINE", "AMANDA", "NICHOLAS", "GARETH", "CHRISTOPHER", "PAUL", "ROBERT", "GAIL", "KEVYN") ;

// select a random element from the array
$random_index = array_rand($names, 1);

// output the index of the random element
var_dump($random_index);

// output the random element
echo $names[$random_index];
?>

Program Output

The output from the above snippet of code, which is dependent on the randomness of the call to array_rand(), might be:

int(3)
NICHOLAS

And an example to return three indices would look like:

The Source Code

<?php
// allocate a small array of strings
$names = array("JUSTIN", "CATHERINE", "AMANDA", "NICHOLAS", "GARETH", "CHRISTOPHER", "PAUL", "ROBERT", "GAIL", "KEVYN") ;

// select some random elements from the array
$random_indices = array_rand($names, 3);

// output the indices of the random elements
var_dump($random_indices);

// output the random elements
echo $names[$random_indices[0]];
echo $names[$random_indices[1]];
echo $names[$random_indices[2]];
?>

Program Output

And the output might be:

array(3) { [0] => int(0) [1] => int(7) [2] => int(9) }
JUSTIN
ROBERT
KEVYN

Notice that in the first example the array_rand() function returned a single, solitary integer, but in the second example the call to array_rand() returned an array of integers. PHP frequently exhibits this inconsistent behaviour and ignores the principal of least surprise.

The odd behaviour stems from the way that array_rand() is called. Requesting a single index or key to be returned will return just a single value. Requesting multiple random indices or keys will return an array of values. It makes sense but it is not logical or consistent.

So far I’ve talked about indices or keys, which when it comes to PHP arrays, is one and the same. We can change the first example above to use an associative array, and then call array_rand() to retrieve random keys.

The Source Code

 

<?php
// allocate a small array of strings
$names = array("a" => "JUSTIN", "b" => "CATHERINE", "c" => "AMANDA", "d" => "NICHOLAS", "e" => "GARETH", "f" => "CHRISTOPHER", "g" => "PAUL", "h" => "ROBERT", "i" => "GAIL", "j" => "KEVYN") ;

// select a random element from the array
$random_index = array_rand($names, 1);

// output the key of the random element
var_dump($random_index);

// output the random element
echo $names[$random_index];
?>

Program Output

Which gives a possible output of:

string(1) "i"
GAIL

Results

The output from the various examples illustrate how to make use of the array_rand() function and the type of results it returns given different scenarios.

Summary

PHP offers a rich variety of array manipulation functions that fulfil about 95% of anything you could ever want to do with arrays. The remaining 5% of situations that PHP does not provide for can usually be constructed with just two or three function calls to the standard PHP function library. When manipulating array data, it is rare to need anything more than the building blocks that PHP provides.

Liked This Post?

Subscribe to the RSS feed or follow me on Twitter to stay up to date!