3

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?

tshepang
  • 12,111
  • 21
  • 91
  • 136

6 Answers6

2

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.

// CREATE A NEW ARRAY TO HOLD ALL OF THE BANNERS
$banner_array = array();     

// LOOP THROUGH EACH ITEM IN THE ARRAY CURRENTLY HOLDING THE BANNERS
foreach ($arr AS $banner) {

    // FOR EACH NUMBER IN THE PRIORITY, ADD THE ITEM TO OUR NEW ARRAY
    // google.com, facebook.com, youtube.com WILL BE ADDED IN TWICE
    for ($i = 0; $i < $banner['priority']; $i++) {
        $banner_array[] = $banner['url'];
    }
}

// COUNT THE TOTAL NUMBER OF ITEMS IN OUR ARRAY
// WE WILL PICK OUT A NUMBER BETWEEN ZERO AND THIS NUMBER (MINUS 1)
$item_count = count($banner_array) - 1;

// ONCE WE HAVE A RANDOM NUMBER, WE CAN ACCESS THAT ITEM OF THE ARRAY
print "RANDOM URL: ".$banner_array[get_random_item($item_count)];


// THIS FUNCTION PICKS A NUMBER BETWEEN ZERO AND THE NUMBER OF ITEMS IN OUR ARRAY
function get_random_item($item_count) {
    mt_srand(microtime() * 1000000);
    $random_number = rand(0, $item_count);
    return $random_number;
}
Quixrick
  • 3,190
  • 1
  • 14
  • 17
1

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:

rand(0, count($keys) - 1);

UPDATE

Here is some code:

// $arr = Your original array

$keys = array();
for ($i = 0; $i < count($arr); $i++) { // you can also use foreach here
     for ($u = 0; $u < $arr[$i]['priority']; $u++) {
         $keys[] = $i;      
     }
}

Then, to fetch a random url, but with priorities, do this:

$arr[ $keys[ rand(0, count($keys) - 1) ] ];
Shomz
  • 37,421
  • 4
  • 57
  • 85
  • Can you give me an example for it, please? (using the array in the top) –  Mar 11 '14 at 19:29
  • Yes, I understood this part, but how to create all the keys? –  Mar 11 '14 at 19:31
  • You can make a new array which would be in format $id => $url_id. $url_id here would be the key of the url in your original array (so google would be 0, facebook 1, etc.). And repeat that multiplied by the priority number (twice for google, facebook, etc... once for the rest). – Shomz Mar 11 '14 at 19:33
  • So you saying that I need to: using foreach check the priority and using while add it to other array `priority` times, and then count() the new array and do random for her? If so, It's greate Idea except I don't know how to add to exist array new "arrays" (I dont know how to call to each one in the array)... EDIT: Thanks, Greate! –  Mar 11 '14 at 19:38
  • Exactly that! I wrote you a small code (not tested, it's from my head), see if that works for you. – Shomz Mar 11 '14 at 19:39
0
$prob = rand(0,9);
if($prob<2){
    //show google url
}else if($prob<4){
    //show facebook url
}else if($prob<6){
    //show youtube url
}else if($prob<7){
    //show stackoverflow url
}else if($prob<8){
    //show kickass url
}else if($prob<9){
    //show twitter url
}else{
    //show example url
}
gtsouk
  • 5,208
  • 1
  • 28
  • 35
  • terrible. how about if theres 200 banners ? – bountyh Mar 11 '14 at 19:24
  • You don't need the `else` in the end... Thanks for the idea, but the array is only an example. As I said, it is an automatic banner system, so there might be 100 websites and priority up to 10 (currently the priority is by Page Rank, but soon it will be other way). –  Mar 11 '14 at 19:26
  • Sorry for that. I thought you might be overthinking this and this simple solution is all you needed! – gtsouk Mar 11 '14 at 19:30
0

Add new field into array, named "prob" that contains the probability of element to show.

$prob = 0;
foreach($arr as $idx => $val) {
  $prob += $arr[$idx]["priority"];
  $arr[$idx]["prob"] = $prob; 
}

After that show the item basing on its priority:

$p = rand(1, $prob);
for ($i=count($array)-1; $i>=0; $i--)
  if ($arr[$i]["prob"] <= $p) {
    // Show this item
    // ...
    break;
  }
Ivan Z
  • 1,517
  • 1
  • 16
  • 25
  • I did not understood how it works `:S`. –  Mar 11 '14 at 19:35
  • You add a new field in your array, named "prob". So the first item has prob=2, the second has prob=4, the third has prob=6 etc. After that you get a random number from 1 to sum of all probs. After that you are looking for an item which prob field is less or equal to random number starting from the end of array. For example, if you random is equal to 5, the code returns you the third item. – Ivan Z Mar 11 '14 at 19:39
0

There is a working example : http://3v4l.org/u5WNS

$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),
 );

// Recreate another array where we have multiple occurence of the same value (nb_of_occurence = priority)

$listOfUrl = array();
foreach ($arr as $url) {
    $nbOfOccurence = $url['priority'];

    for($i = 0 ; $i < $nbOfOccurence ; $i++) {
        $listOfUrl[] = $url['url'];
    }
} 

// Count the number of elements in this new array

$nbOfElement = count($listOfUrl);

// Generate a random index between 0 and (number of element - 1)

$randomIndex = rand(0,($nbOfElement - 1));

// Retrive the random value

$randomURL = $listOfUrl[$randomIndex];

echo $randomURL;
Frederic Nault
  • 986
  • 9
  • 14
0
$prob_arr = array();
$count=0;
foreach($arr as $key=>$val){
    for($j=0;$j<$val;$j++){
        $prob_arr[$i] = $key;
        $count++;
    }
}

$banner = prob_arr[rand(0,$count-1)];
gtsouk
  • 5,208
  • 1
  • 28
  • 35