What is the difference between shuffle() and array_rand() in PHP?



What is the difference between shuffle() and array_rand() in PHP? | ninjasquad

What exactly is the difference between shuffle and array_rand functions in PHP?

void shuffle ( array input)

mixed array_rand ( array input [, int num_req])

shuffle()

shuffle() takes an array and completely randomizes the array. You may have noticed that shuffle() uses its parameter by reference and returns true.

The downside of this function is it changes the orders of the array keys. However, if it’s a crucial part of your project, go for it.

$fruits = array("banana", "grapes", "apple", "orange");
var_dump($fruits);
shuffle($fruits);
var_dump($fruits);

Before

array(4) {
  [0]=>
  string(6) "banana"
  [1]=>
  string(6) "grapes"
  [2]=>
  string(5) "apple"
  [3]=>
  string(6) "orange"
}

After

array(4) {
  [0]=>
  string(6) "grapes"
  [1]=>
  string(6) "orange"
  [2]=>
  string(6) "banana"
  [3]=>
  string(5) "apple"
}

array_rand()

On the other hand,  array_rand() picks a random key from the array, but the array remains untouched. If you don’t specify the key it will generate the numerical index.

For example, follow the below example,

$fruits = array("banana", "grapes", "apple", "orange");
var_dump($fruits);
$out = array_rand($fruits);
var_dump($out);

array(4) {
  [0]=>
  string(6) "banana"
  [1]=>
  string(6) "grapes"
  [2]=>
  string(5) "apple"
  [3]=>
  string(6) "orange"
}
int(1)

array_rand() has an optional second parameter that allows you to decide on the number of keys returned from the operation. If you don’t specify the second parameter, you will get a standard variable back, otherwise an array.

$fruits = array("banana", "grapes", "apple", "orange");
var_dump($fruits);
$out = array_rand($fruits,2);
var_dump($out);

array(4) {
  [0]=>
  string(6) "banana"
  [1]=>
  string(6) "grapes"
  [2]=>
  string(5) "apple"
  [3]=>
  string(6) "orange"
}
array(2) {
  [0]=>
  int(0)
  [1]=>
  int(1)
}

For example, if you use array_rand($fruits, 2), it will return two random fruits. However, there is a case it will return only the unique values. If you have two same keys, it will never return the same keys. Moreover, the second parameter value can not be more than the unique array values.

For example, the below case will throw an error as the number of unique keys in the array is 3, but you have passed 4 as the second parameter.

$fruits = array("key1"=> "banana", "key2"=>"grapes", "key3"=>"apple", "key2"=>"orange");
var_dump($fruits);
$out = array_rand($fruits,4);
var_dump($out);
Fatal error: Uncaught ValueError: array_rand(): Argument #2 ($num) must be between 1 and the number of elements in argument #1 ($array) in /home/user/scripts/code.php:5
Stack trace:
#0 /home/user/scripts/code.php(5): array_rand(Array, 4)
#1 {main}
  thrown in /home/user/scripts/code.php on line 5

Hope it helps, please share with your peers.


Editorial Staff

Editorial Staff at Tutsplanet is a dedicated team to write various tutorials about subjects like Programming, Technology and Operating Systems.



Source: Internet

Leave a Comment

We are offering free coding tuts

X