You are not logged in.
Login
or
Register
.
JsPHP: a JavaScript library providing the PHP API
Home
Register
Demo
Categories
Functions
Contributors
Download
Links
Comments
FAQ
Contact
View
Edit
Benchmark
Revisions
Developers
Dependencies
Talk
Links
array_rand:
Edit: array_rand
Function:
array_rand
View array_rand
Edit array_rand
Edit code
Edit tests
Edit benchmark
View latest code
Edit latest code
Benchmark array_rand
Comment on array_rand
Comment on the latest code
View revisions
View versions
View contributors
View dependencies
View comments
View links
Download production code
Download development code
View the PHP docs
View phpjs.org
View phpjs.org raw code
Description:
Return key/keys for random entry/entries in the array.
Edit function
Code
:
function array_rand (input, num_req) { // http://jsphp.co/jsphp/fn/view/array_rand // + original by: Waldo Malqui Silva // * example 1: array_rand( ['Kevin'], 1 ); // * returns 1: 0 var indexes = []; var ticks = num_req || 1; var checkDuplicate = function (input, value) { var exist = false, index = 0, il = input.length; while (index < il) { if (input[index] === value) { exist = true; break; } index++; } return exist; }; if (Object.prototype.toString.call(input) === '[object Array]' && ticks <= input.length) { while (true) { var rand = Math.floor((Math.random() * input.length)); if (indexes.length === ticks) { break; } if (!checkDuplicate(indexes, rand)) { indexes.push(rand); } } } else { indexes = null; } return ((ticks == 1) ? indexes.join() : indexes); }
You have to be logged in to edit functions.
Edit tests
Code
:
You have to be logged in to edit tests.
Edit benchmark
Code
:
You have to be logged in to edit benchmarks.
[
top
]
Comments
There are no comments yet, be the first!
Please
Login
or
Register
to post comments.