Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

array_rand(3) [php man page]

ARRAY_RAND(3)								 1							     ARRAY_RAND(3)

array_rand - Pick one or more random entries out of an array

SYNOPSIS
mixed array_rand (array $array, [int $num = 1]) DESCRIPTION
Picks one or more random entries out of an array, and returns the key (or keys) of the random entries. PARAMETERS
o $array - The input array. o $num - Specifies how many entries should be picked. RETURN VALUES
When picking only one entry, array_rand(3) returns the key for a random entry. Otherwise, an array of keys for the random entries is returned. This is done so that random keys can be picked from the array as well as random values. Trying to pick more elements than there are in the array will result in an E_WARNING level error, and NULL will be returned. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ |5.2.10 | | | | | | | The resulting array of keys is no longer shuf- | | | fled. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 array_rand(3) example <?php $input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank"); $rand_keys = array_rand($input, 2); echo $input[$rand_keys[0]] . " "; echo $input[$rand_keys[1]] . " "; ?> SEE ALSO
shuffle(3). PHP Documentation Group ARRAY_RAND(3)

Check Out this Related Man Page

ARRAY_KEYS(3)								 1							     ARRAY_KEYS(3)

array_keys - Return all the keys or a subset of the keys of an array

SYNOPSIS
array array_keys (array $array, [mixed $search_value], [bool $strict = false]) DESCRIPTION
array_keys(3) returns the keys, numeric and string, from the $array. If the optional $search_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the $array are returned. PARAMETERS
o $array - An array containing keys to return. o $search_value - If specified, then only keys containing these values are returned. o $strict - Determines if strict comparison (===) should be used during the search. RETURN VALUES
Returns an array of all the keys in $array. EXAMPLES
Example #1 array_keys(3) example <?php $array = array(0 => 100, "color" => "red"); print_r(array_keys($array)); $array = array("blue", "red", "green", "blue", "blue"); print_r(array_keys($array, "blue")); $array = array("color" => array("blue", "red", "green"), "size" => array("small", "medium", "large")); print_r(array_keys($array)); ?> The above example will output: Array ( [0] => 0 [1] => color ) Array ( [0] => 0 [1] => 3 [2] => 4 ) Array ( [0] => color [1] => size ) SEE ALSO
array_values(3), array_key_exists(3), array_search(3). PHP Documentation Group ARRAY_KEYS(3)
Man Page

12 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Lets give Neo a hand.

Neo, Just wanted to let you know that we all appricate the hardwork that you and your team put in to make this forum what it is. I have been a member since 05-23-2001 and i can honestly say this is one forum that is deffinetly a main stay and a true benifit to everyone that uses it. Mike... (4 Replies)
Discussion started by: Optimus_P
4 Replies

2. Shell Programming and Scripting

Array operation

Hi, I would like ask for you help for coding array operation. array= ( a b c d e f ) I would like to remove entry "d" from my array and import the remaining entries back to the array. Thanks. (3 Replies)
Discussion started by: phamp008
3 Replies

3. UNIX for Dummies Questions & Answers

Intermittent problem reading from an input file.

First of all thanks to all for the good post, and the great site. I'm a noob, but I've been able to learna a lot by checking past posts. I haven't been able to make sense of a problem that I've been working on for a while, hopefully someone can help me out. The script I wrote telnets into... (7 Replies)
Discussion started by: Wallygooo32
7 Replies

4. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

5. Shell Programming and Scripting

Pick random file from ls command.

Lets say I want to pick a random file when I do an "ls" command. I don't have set number of files in each directory. ls | head -1 This gives me the first one in each directory, is there a way to do the same but pick a random one. (3 Replies)
Discussion started by: elbombillo
3 Replies

6. Shell Programming and Scripting

space not allowed in input

I have this script. (options is an array) It works fine. It responds fine to all items that are stored in the array. But when I press spacebar when I am asked for input. It will not give an error only run the function again. While I put : || ] in the script to capture the space. What am I missing... (6 Replies)
Discussion started by: Alex400
6 Replies

7. Shell Programming and Scripting

randomly shuffle two text files the same way

What I have are two text files that I need to shuffle randomly, but I need the two files to be randomly shuffled the same way. I have heard of shuf but I do not know how to use it for two files. Maybe there is also an easy/simple awk command I do not know about that could handle this problem. ... (3 Replies)
Discussion started by: adrunknarwhal
3 Replies

8. Shell Programming and Scripting

Diff command file entries in different lines

Hello, I have two files to compare these contain some contents like this : FIle 1 : A B C E File 2 has some new entries and the old entries are in some different ordre File 2 could be like this : C E A B G I (4 Replies)
Discussion started by: ajayram
4 Replies

9. What is on Your Mind?

Raspberry PI

The small red box to the left of TV is the Raspberry PI. Successfully installed and running Raspbian Wheezy. I learnt about Raspberry PI from Neo here on unix.com. Thanks to you Neo :b: (6 Replies)
Discussion started by: balajesuri
6 Replies

10. Shell Programming and Scripting

Failure using regex with awk in 'while read file' loop

I have a file1.txt with several 100k lines, each of which has a column 9 containing one of 60 "label" identifiers. Using an labels.txt file containing a list of labels, I'd like to extract 200 random lines from file1.txt for each of the labels in index.txt. Using a contrived mini-example: $ cat... (8 Replies)
Discussion started by: pathunkathunk
8 Replies

11. What is on Your Mind?

Happy Birthday Neo ( Tim Bass )

(12 Replies)
Discussion started by: Akshay Hegde
12 Replies

12. Programming

Code review: recursion in circular array, reduce two functions to one?

Hello, I think there's an easier way to do this but can't seem to recall but given an array of animals and an initial value is a random index in the array, here it's 3. 3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0... inifinite repeat a quick brute force solution i came up with was two functions, i... (6 Replies)
Discussion started by: f77hack
6 Replies