Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Problem with shuffle() function

    Hi,
    I am using shuffle() to find a random item in an array.

    The problem is that the array is generated from client entries and it
    is possible that the entries can be the same value. In that case I can end up with an array like this:

    Array ( [0] => EBFORTUNE [1] => EBFORTUNE [2] => EBFORTUNE [3] => EBFORTUNE )


    In that case when the shuffle runs I get
    a 500 Internal error

    Is this normal?

    How can I overcome it - or check that it is safe to run the
    shuffle function ?

    Thanks.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    The shuffle function wont be causing the 500 internal error. This is a php function so it would just spit out a PHP error if there was anything wrong. I'd suggest contacting your host as this is an APACHE issue
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    OK,

    I have just realized why I am getting the error.

    This is my code:

    PHP Code:
    shuffle($cb_promo_hits);
    $hit_1 $cb_promo_hits[0];
    $hit_2 $cb_promo_hits[1];
    while ( 
    $hit_2 == $hit_1){
        
    shuffle($cb_promo_hits);
        
    $hit_2 $cb_promo_hits[0];

    It is because I want two results from the array and I want the
    them to be different.

    So when there is no different item
    - I produce a never ending loop

    Now my problem is - what can I do about it ?

    In this situation I would accept the two
    variables to be the same ( as there is no other choice).

    But how can I test for this ?

    Is there a way to test the array for
    differing values ?

    Any help appreciated, thanks
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Use this code
    PHP Code:
    if(count(array_count_values($array_here)) > 1) {
        
    // CODE HERE FOR MORE THAN ONE VALUE
    }else{
        
    // CODE HERE FOR ONE VALUE ONLY

    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    How about removing duplicate values using array_unique(), before calling shuffle? (You'd need to ensure that there's are least two entries, to proceed with rest of your code)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    The problem with array_unique is that it will remove those from the original array, and the OP may not want to
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by JAY6390 View Post
    The problem with array_unique is that it will remove those from the original array, and the OP may not want to
    No it doesn't, array_unique returns an array with all elements minus the duplicates.

    PHP Code:
    $unique array_unique($cb_promo_hits);
    shuffle($unique);
    $hit_1 $cb_promo_hits[0];
    $hit_2 $cb_promo_hits[1]; 
    Edit:
    array_merge isn't necessary, I wasn't certain if shuffle reindexed, but it does.
    Last edited by Fou-Lu; 01-25-2010 at 01:02 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #8
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    shuffle$cb_promo_hits );

    $hit_1 array_shift$cb_promo_hits );

    do
    {
        
    $hit_2 array_shift$cb_promo_hits );
    }
    while ( ! empty( 
    $cb_promo_hits ) && $hit_1 == $hit_2 ); 
    It only needs to be shuffled once. If they are still the same value and there are no items left to pull from the array, the loop will cease.
    Last edited by kbluhm; 01-25-2010 at 01:07 PM. Reason: semi-colon

  • #9
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Oops, so it does. My apologies abduraooft
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #10
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    A Real big thanks for
    all your imput.

    I can not use the unique methods because I actually want
    to allow the duplicates !

    As I mentioned in my post: the array "is generated from client entries"
    and the more that enter they get a higher chance of being selected.

    ( I mean if someone has four horses in the race they are more likely to have a winner)

    I like this array_shift() function here:

    PHP Code:
    shuffle$cb_promo_hits );

    $hit_1 array_shift$cb_promo_hits );

    do
    {
        
    $hit_2 array_shift$cb_promo_hits );
    }
    while ( ! empty( 
    $cb_promo_hits ) && $hit_1 == $hit_2 ); 
    This looks fine, but does it solve my
    problem about when all four entries in the
    array are the same ?

    I will test it but it looks to me like
    it wont give me a $hit_2 in the situation
    that causes my problem ... or does it ?


    .
    ADDED:

    I guess that you assume that I use JAYs
    suggestion first to do the checking ?
    then change my shuffle to the above.


    So I end up with:

    PHP Code:
    if(count(array_count_values($cb_promo_hits)) > 1) {
       
    shuffle$cb_promo_hits );
       
    $hit_1 array_shift$cb_promo_hits );

         do {
        
    $hit_2 array_shift$cb_promo_hits );
            }
            while ( ! empty( 
    $cb_promo_hits ) && $hit_1 == $hit_2 );  
            }
    else{
        
    $hit_1 $cb_promo_hits[0];
        
    $hit_2 $cb_promo_hits[1];
            } 



    .
    Last edited by jeddi; 01-26-2010 at 06:18 AM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •