0

this is my first php script and problem, I've searched hours with no conclusion other than looping a function" too many laterations". but it doesn't solve my problem I've never studied programming or what ever so I'm hoping that there is an educated person to fill me in on this:

I have an array that contains 120 elements; consists of duplicates eg:

myArray = [0]= item_1, [1] = item _1, [2] = item_2, [3] = item_3 ect..

Briefly I'm trying to make a flash php pokermachine but I need these items in the array to be shuffled BUT I do not want the duplicates to be next to each other after the shuffle but I need the duplicates to be still in the array

I can't do a loop function to check this because it will change the shuffle too many times which will effect the odds of the game: below is what I currently have:

/ * Removed the link here that is no longer available */

you may notice at times it will double up with 2 items in the same reel Basically I created the virtual reel dynamically with php.ini file these values are repeatedly pushed into an array($virtualreel) so the value may appear 10 times in the reel and another value will appear 5 times variating the odds. Then after I take a random slice() from the $virtualreel to display 3 vars from this reel and repeat the loop 4 more times for the other reels, also I only can shuffle once as I want the slice() to be from the same reels array order

I only shuffle every new spin not running loop functions to shuffle if I double up on a slice(array,3 items).

hope I've explained what I'm after well enough to give you guys an idea.

2 Answers 2

1

You can use this function:

<?php

function shuffleArray($myArray) {
  $value_count = array_count_values($myArray);
  foreach($value_count as $key=>$value) {
      if ($value > count($myArray)/2) {
           return false;
      }
  }
  $last_value = $myArray[count($myArray) - 1];
  unset($myArray[count($myArray) - 1]);
  $shuffle = array();
  $last = false;
  while (count($myArray) > 0) {
    $keys = array_keys($myArray);
    $i = round(rand(0, count($keys) - 1));
    while ($last === $myArray[$keys[$i]]) {
        $i = round(rand(0, count($keys) - 1));
    }
    $shuffle[] = $myArray[$keys[$i]];
    $last = $myArray[$keys[$i]];
    unset($myArray[$keys[$i]]);
  }

  if ($last_value === $last) {
    $i = 0;
    foreach($shuffle as $key=>$value) {
        if ($value !== $last_value) {
            $i = $key;
            break;
        }
    }
    array_splice($shuffle, $i + 1, 0, $last_value);
  } else {
    $shuffle[] = $last_value;
  }

  return $shuffle;
}

print_r(shuffleArray(array(1,5,5,3,7,7)));
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for your reply, it works with your example well .I will try to incorparate this method into my own project.thanks for solving this issue.
ok i have used your method with success, BUT the script quiet oftens times out, and at regular times doesnt shows anything. maybe they could be a more practical solution to achieve this solution.
Can you give me example of $myArray values to check it.
I worked out the problem, but cant solve it as im not that familiar with php. So i believe why it throws an error at random is becuase the function hangs on part of the function. im guessing:EXAMPLE $array = [0]= apple, [1] = apple, [2] = apple, [3] = orange; basically no matter how you shuffle the array always the "apple" will be next to a "apple" at the end of the array so im thinking a may need to a bit of code in the function to check if this happens
so my next question would be to ask in your example how to break this loop if the remainding values are the same to stop trying to shuffle the remainder items for ever and continue/ break from loop
|
1

Why not just:

Edit :

$shuffled = array();
while(count($to_shuffle) > 0):
    $i = rand(0, count($to_shuffle)-1);
    $shuffled[] = $to_shuffle[$i];
    array_splice($to_shuffle, $i, 1,null);                  
endwhile;

I think this is what you were expecting, if you don't mind not preserving the association between keys and values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.