Return a random key from an array in PHP

Description

The following code shows how to return a random key from an array.

Example


//  www . j a  va  2 s  .c o m
<?php
    $a=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
    print_r(array_rand($a,1));
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone