Tuesday 4 August 2015

Get Random Values From Array IN PHP

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

Create .ICS file using PHP code

Recently worked on creating a .ics file in PHP after a very long time, code so thought to share with everybody. Please find below the comple...