How to get a random key from a PHP array – onlinecode
In this post we will give you information about How to get a random key from a PHP array – onlinecode. Hear we will give you detail about How to get a random key from a PHP array – onlinecodeAnd how to use it also give you demo for it if it is necessary.
In this post, i will tell you how to get random key from PHP array.
array_rand
method in PHP will return the keys of the random entries.
This method takes two argument : first will be the input array and second argument specifies how many entries you want to picked.
Each time when you refresh the page you will get different number.
Syntax :
array_rand(array,number)
Example :
- <?php
- $arr=array('a','b','c','d','e','f');
- $random_key=array_rand($arr,3);
- print_r($random_key);
- ?>
<?php $arr = array('a','b','c','d','e','f'); $random_key = array_rand($arr, 3); print_r($random_key); ?>
Try above example when you need to get random key from given array.
Hope this code and post will helped you for implement How to get a random key from a PHP array – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs