This is my code, for example:
<?php
$arr = array(
array("url" => "http://google.com", "priority" => 2),
array("url" => "http://facebook.com", "priority" => 2),
array("url" => "http://youtube.com", "priority" => 2),
array("url" => "http://stackoverflow.com", "priority" => 1),
array("url" => "http://kickass.to", "priority" => 1),
array("url" => "http://twitter.com", "priority" => 1),
array("url" => "http://example.com", "priority" => 1),
);
?>
I want the system to randomly display one of the url's on each refresh. I want it to display the higher priority more times than the lower. I need it for banner system, and the ones with higher priority paying more, so they should be seen more.
How to do it?
Add new field into array, named "prob" that contains the probability of element to show.
After that show the item basing on its priority:
There is a working example : http://3v4l.org/u5WNS
// Recreate another array where we have multiple occurence of the same value (nb_of_occurence = priority)
// Count the number of elements in this new array
// Generate a random index between 0 and (number of element - 1)
// Retrive the random value
Iterate over all the banners, and assign a key (numeral id) for each. To those with a higher priority, assign 2 keys (or more if you wish an even higher priority). Then just find the random number between 0 (assuming it's zero-based) and the total number of keys:
UPDATE
Here is some code:
Then, to fetch a random url, but with priorities, do this:
You can add items to an array based on their priority. If an item has a priority of 2, you can add it to the array twice. Then you can pull out a random item from the array.