Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,941 questions

51,879 answers

573 users

How to get random entries (indexes) out of an array in PHP

2 Answers

0 votes
$arr = array("PHP", "C", "C++", "Java", "C#", "JavaScript");
$rand_entries = array_rand($arr, 3);
echo $arr[$rand_entries[0]] . "<br />";
echo $arr[$rand_entries[1]] . "<br />";
echo $arr[$rand_entries[2]] . "<br />";


/*
run:

C
Java
JavaScript
 
*/

 



answered Jul 31, 2015 by avibootz
0 votes
$arr = array("PHP", "C", "C++", "Java", "C#", "JavaScript");
$rand_entries_arr = array_rand($arr, 3);

foreach ($rand_entries_arr as $i)
        echo $arr[$i] . "<br />";


/*
run:

PHP
C
JavaScript
 
*/

 



answered Jul 31, 2015 by avibootz

Related questions

1 answer 161 views
161 views asked Jul 16, 2016 by avibootz
1 answer 229 views
1 answer 96 views
2 answers 175 views
1 answer 123 views
...