• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Tutsplanet

Tutsplanet

Free Technical and Blogging Resources

  • Home
  • Web Hosting
  • Programming
  • Plugins
  • Twitter Trends
  • Tools
  • About Us

Programming TagsPHP

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

Sep 20, 2022 Editorial Staff Leave a Comment

Share
Tweet
Share

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.

View all posts by Editorial Staff

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Quick Links

  • Top 21 Website Ideas To Make Money Online in 2021
  • A Simple YouTube Video Downloader Script in PHP
  • The 50 Most Useful jQuery Plugins for Frontend Development
  • Replace “\n” with new line characters, using Notepad++
  • Using Third-Party Libraries in Codeigniter
  • Upload Multiple Images and Store in Database using PHP and MySQL.
  • Hierarchical Tree view Category Example in Laravel
  • Laravel Image Intervention Tutorial With Example
  • How to import sql file in MySQL database using PHP?
  • Free VAT Calculator Online

Subscribe

* indicates required



Search Here

Share

   



Hot topics

  • Replace “\n” with new line characters, using Notepad++ 41 views
  • How to enter new line in Microsoft Teams? 35 views
  • Simple PHP Shopping Cart 18 views
  • Open a URL in a new tab using JavaScript 16 views
  • Add FTP/SFTP in Visual Studio Code 15 views
  • How to Create PDF File From HTML Form Using Fpdf? 14 views
  • Laravel: Download files to storage from SFTP 12 views
  • Solution: windows photo viewer opens each time save a pic file 11 views
  • Laravel Eloquent Select Column as Alias 11 views
  • A Simple YouTube Video Downloader Script in PHP 11 views

Categories

  • Design & Development
  • Drupal
  • Facebook
  • General
  • How To
  • ios
  • Javascript
  • Linux
  • Magento
  • Marketing
  • News
  • PHP
  • Plugins
  • Programming
  • Snippets List
  • Social Media
  • Softwares
  • Themes
  • Tips
  • Wordpress
  • YouTube























Copyright © 2023 · Planet on Genesis Framework · Powered By BunnyCDN . Network wallpapernoon.com