SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,643
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    randomize an array?

    I have 3 arrays, $list1, $list2, and $list3. Each of the three has 10 elements, Im trying to randiomize 1 array,
    PHP Code:
    shuffle($list1); 
    But how do I make it so that the new order is carried over to the other two arrays?
    You can see the three arrays printed out

    http://shores-rentals.com/luth/question8.php

    Thanks...
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,153
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    Use the numerical key values from the first for the others.

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    One way would be to set up an extra array and shuffle that.

    Code:
    $list0 = array(0,1,2,3,4,5,6,7,8,9);
    shuffle($list0);
    Now the numbers in this extra array can be used as the index into the other three arrays so that you access the corresponding entries in all three of your original arrays without changing their order at all but simply changing the order in which you access the entries from sequential to the random order specified in the extra array.

    Code:
    for ($i = 0; $i < 10; ++$i) {
       echo $list1[$list0[$i]].' : '.$list2[$list0[$i]].' : '.$list3[$list0[$i]].'<br>';
    }
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,643
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanks felgall (ill try that)

    How would I grab the key values if this is how to randomize the array,
    PHP Code:
    shuffle($list1); 
    "Oh, and Jenkins--apparently your mother died this morning."

  5. #5
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,643
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I created a master array, but now whewn I try to pring out each arrqay like
    PHP Code:
    echo "<ol>";
    foreach(
    $list1 as $value) {
    echo 
    "<li>".$master[$value]."</li>";
    }
    echo 
    "</ol>";
    echo 
    "</td><td>";
    echo 
    "<ol>";
    foreach(
    $list2 as $value) {
    echo 
    "<li>".$master[$value]."</li>";
    }
    echo 
    "</ol>";
    echo 
    "</td><td>";
    echo 
    "<ol>";
    foreach(
    $list3 as $value) {
    echo 
    "<li>".$master[$value]."</li>";
    }
    echo 
    "</ol>";
    echo 
    "</td></tr>";
    echo 
    "</table>"
    I get nothing
    "Oh, and Jenkins--apparently your mother died this morning."

  6. #6
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,643
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thank you, that worked, the thing that's odd to me is the for loop, why do you have ++$i and not $i++?
    "Oh, and Jenkins--apparently your mother died this morning."

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,122
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lukeurtnowski View Post
    thank you, that worked, the thing that's odd to me is the for loop, why do you have ++$i and not $i++?
    This ought to give you the insight behind that
    http://stackoverflow.com/questions/2...etween-i-and-i


Bookmarks

Posting Permissions

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