How to get randomly 10 elements form array in PHP?

Votes : 3.6
Get more than 10 users in my program. I want to limit to 10 users randomly. How can i do this? Array ( [1] => 'John', [2] => 'Anthony', [3] => 'Iris', [4] => 'Schaffer', ... )

Your Answer

1 Answer

You can use to shuffle an array from array_rand or shuffle PHP functions to get random order list.

array_rand ( $array, $length )

$array - ( required ) Specifies an array.
$length - ( Optional ) Default value is 1. Specifies how many random keys to return.
Returns a random key from an array, or an array of random keys if you specify that the function should return more than one key.
$user1 = array( 'John Lewis', 'Mark Antony', 'Iran Prasantha', 'Dilip Kumara' ); $user2 = array( 'john' => 'John Lewis', 'anthony' => 'Mark Antony', 'iran' => 'Iran Prasantha', 'dilip' => 'Dilip Kumara', ); var_dump(array_rand($user1)); // int 1 if(count($user1) >= 3){ var_dump(array_rand($user1,3)); // array (size=3) [2,0,1] } var_dump(array_rand($user2)); //string 'anthony' if(count($user2) >= 2){ var_dump(array_rand($user2,2)); // array (size=2) ['anthony','john'] }
Notice : You can get only random list by using array_rand function, If $array count more than $length value.
Now, you can see these results. It's already shuffle and limited, But the results are not completed because results are only contain array keys. We have to get array keys with values. // Randomize users list then get 10 keys from randomize array. $r_users = array_rand($user2,2); // array (size=4) ['dilip','iran'] $users = array(); foreach($r_users as $user){ if(isset($user2[$user])) $users[$user] = $user2[$user]; } var_dump($users);

shuffle ( $array )

$array - ( required ) Specifies an array.
Returns TRUE on success or FALSE on failure.
// ( PHP 4, PHP 5, PHP 7 ) shuffle($user1); // shuffle array. var_dump($user1); // array (size=4) ['John Lewis','Dilip Kumara','Mark Antony','Iran Prasantha'] $user3 = $user2; shuffle($user3); // shuffle array. var_dump($user3); // array (size=4) ['Dilip Kumara','John Lewis','Iran Prasantha','Mark Antony'] // Did you see $user3 shuffle result.It has been return values not with keys. How can we fix this? $user4 = array_keys($user2); shuffle($user4); var_dump($user4); // array (size=4) ['john','dilip','iran','anthony']

Now, arrays shuffled successfully. Then we need to limit to get 10 elements from a one array, and also need to get both values with keys from $user3 array.

var_dump(array_slice($user1,0,10)); // array (size=4) ['John Lewis','Mark Antony','Dilip Kumara','Iran Prasantha'] var_dump(array_slice($user1,0,2)); // array (size=2) ['Iran Prasantha','Mark Antony'] $user4 = array_slice($user4,0,10); $users = array(); foreach($user4 as $user){ if(isset($user2[$user])) $users[$user] = $user2[$user]; } var_dump($users);// array (size=4) ['john' => 'John Lewis', 'dilip' =>'Dilip Kumara', 'iran' =>'Iran Prasantha', 'anthony' =>'Mark Antony']

Our solution

This is my solution to get random values form an array. function es_randomArray($array, $length = 1) { /* * Get random values from an array. * @param (array|required) $array - an array to get random values. * @param (integer|optional) $length - number of values do need to get? * * @return (array) random values. * * source : */ if (is_array($array) && !empty($array)) { shuffle($array); $return = array(); $keys = array_slice(array_keys($array), 0, $length); foreach ($keys as $key) if (isset($array[$key])) $return[$key] = $array[$key]; } return $return; } eg: var_dump(es_randomArray($user1,3));// array (size=3) ['Dilip Kumara', 'Mark Antony', 'Iran Prasantha'] var_dump(es_randomArray($user2,2));// array (size=3) ['dilip' =>'Dilip Kumara', 'anthony' =>'Mark Antony']

What is the codrate ? is a standard, fast cross browsing and highly versatile site. It is useful for many large number of Program Development Industries. So you can get support form Codrators , who are the codrate's joiners around world to help your program developments, You can answer other codrator's questions. Communicate with them. Share your knowledge with them. Do you have an interest in programming, So publish your articles about programming. It will help to maintain your professional co-profile. Actually is not such as a regular web site. It will be gave new experience, best narrow cross-browser view, reduce processing time to receive browsing request, it's mean do not wasting your time to browsing codrate's web pages because it has been upgrade always modern coding ways. So, what do you waiting for ?. Try your own.

Copyright 2015 Pride - Company. Design by Esila