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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Apr 2010
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts

    How do you shuffle arrays? (i.e. between $arr1, $arr2 or $arr3)

    I have 3 arrays:

    $arr1
    $arr2
    $arr3

    and I want $arr1 to show up twice more than $arr2 and 3. I wrote this:

    Code:
    $shuffle = '$arr3#$arr2#$arr1#$arr1'; 
    	$shuffle = explode('#', $shuffle); 
    	shuffle($shuffle); 
    	$shuffle = implode('#', $shuffle);
    	$shuffle2 = substr($shuffle, 0, strpos($shuffle, '#')).'';
    
    echo $shuffle2;
    but all it does is display the TEXT "$arr3". It never displays the arrays (which are all text strings e.g. one is a quote randomiser).

    $arr1, 2 and 3 all show up their resulting text when echoed individually, so all their programming works fine. But I only want to display one of those arrays at a time and $arr1 more than the other two.
    Last edited by instaunt; 04-28-2013 at 02:30 PM.
    I've been making animations for my daughter. Tell me what you think!

    Pickles and the Bully
    Where are you Pickles!?!

  • #2
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    PHP Code:
    $arr1 = array('a1''a2''a3');
    $arr2 = array('b1''b2''b3');
    $arr3 = array('c1''c2''c3');

    //marge arrays
    $arrMarged array_merge($arr1$arr1$arr2$arr3);

    //shuffle
    shuffle($arrMarged);

    //output "random" element
    echo $arrMarged[0]; 
    but this will work only if you don't specyfy keys in your arrays.
    and if understood correctly what you want to do.... (marge arrays, shuffle all together and display one random element)
    Last edited by patryk; 04-28-2013 at 03:14 PM.

    -------------------------------------------------------------------------------
    "Real Programmers can write assembly code in any language" - Larry Wall

  • #3
    Regular Coder
    Join Date
    Apr 2010
    Posts
    118
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thank you, but no. Two of the arrays are not simply sets of strings. Some are the result of string snipping, str_replacing, inserting other text under certain circumstances etc.

    So I really need to shuffle $arr1, $arr2 and $arr3 (i.e. the resulting output from each piece of work).
    I've been making animations for my daughter. Tell me what you think!

    Pickles and the Bully
    Where are you Pickles!?!

  • #4
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    could you show examples of those arrays ( print_r() )

    -------------------------------------------------------------------------------
    "Real Programmers can write assembly code in any language" - Larry Wall


  •  

    Posting Permissions

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