Get Random Values From Array IN PHP
I am sharing my example code to get random values from PHP array. May this small code can be helpful for someone.
<?php
// array
$arr_subjects = array('Science', 'Maths', 'GK', 'Law', 'History', 'Arts', 'Chemistry',
'Computers', 'Bio', 'Music');
// random number
$c = rand(1, 6);
//shuffle array elements
shuffle($arr_subjects);
//print random values in an array
print_r(array_slice($arr_subjects, $c, $c));
// prints $c'th elements from $c'th position in an array
No comments:
Post a Comment