Hi, I have this banners rotator script:

<?php
//randomly shuffle the array keeping the relation between keys and values
function shuffle_me($shuffle_me){
$randomized_keys = array_rand($shuffle_me, count($shuffle_me));
foreach($randomized_keys as $current_key) {
$shuffled_me[$current_key] = $shuffle_me[$current_key];
}
return $shuffled_me;
}
$center_banners=shuffle_me($center_banners);
$center_banners=array_slice($center_banners, 0, 20);

$left_banners=shuffle_me($left_banners);
$right_banners=shuffle_me($right_banners);
?>

OK, the script runs perfectly at a PHP4 server, but it doesn't work at my current PHP5 server.

I think that the reason is the change at the array_rand log:
5.2.10 - The resulting array of keys is no longer shuffled.

What exactly do I need to change at the script to work at PHP5? As the script is part of a topsite, it is interrelated with multiple programming pages so the change should be the minimum necessary to work with PHP5.

I have done multiple test without results. Consider that my PHP skills are so low. Please, help me!

Maria

    Surely if the function array_rand() no longer shuffles the keys into a random order, then this function is most likely not what you will want to use in PHP5.

      5 days later

      You can use the shuffle function to preserve the keys if you need to - with a little creativity. This example is on the shuffle() manual reference page... (Untested, but the code looks like it should work to serve your needs).

      function kshuffle(&$array) {
          if(!is_array($array) || empty($array)) {
              return false;
          }
          $tmp = array();
          foreach($array as $key => $value) {
              $tmp[] = array('k' => $key, 'v' => $value);
          }
          shuffle($tmp);
          $array = array();
          foreach($tmp as $entry) {
              $array[$entry['k']] = $entry['v'];
          }
          return true;
      }
      
      // Usage (assuming your array is named "$shuffle_me")
      kshuffle($shuffle_me);
      
        Write a Reply...