PHP

PHP Menu

PHP

array_rand() Function - Definition, Syntax, Parameters, Examples

Definition

The array_rand() function returns a random key from an array, or it returns an array of random keys if you specify that the function should return more than one key.

Syntax

array_rand(array, number)

Parameters

Parameter Description
array Required. Specifies an array.
number Optional. Specifies how many random keys to return.

Example

<?php
// Example 1 $cities = array("New York", "Salt Lake", "Tokyo"); $rand_keys = array_rand($cities,2); print_r($rand_keys); echo "<br>";
// Example 2 $ages = array("Mark" => 22, "Jeff" => 32, "Mike" => 28); $rand_keys = array_rand($ages,2); print_r($rand_keys); echo "<br>";

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods