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 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2013
    Posts
    38
    Thanks
    5
    Thanked 0 Times in 0 Posts

    array_rand use in while loop

    Hello, I'm new to php/coding in general and have recently been offered some terrific code in response to a question:

    http://stackoverflow.com/questions/1...itch-sequences

    ... but I'm still trying to understand it, and this forum seems better place for open-ended discussion than SO.

    The main thing I'm having trouble following is this line (from Pudge601):
    $dist = $dists[$index][array_rand($dists[$index])];
    ... which seems to (paradoxically) randomly create the first level of the stated array as the second level within the same array.

    Could anyone explain what is going on there in 'English'? I suspect if I could really grok that like the rest of the script would fall into place. Thanks!

  • #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    something like :

    $dists = array(
    array(array(3,3,3)),
    array(array(4,3,3),array(3,4,3),array(3,3,4)),
    array(array(4,4,3),array(4,3,4),array(3,4,4)),
    array(array(4,4,4),array(3,3,3,3))
    );

    $dists is basically

    $dists[1][0][0] = 4
    $dists[1][0][1] = 3
    $dists[1][0][2] = 3
    ...
    etc

    $dist = $dists[$index][array_rand($dists[$index])];
    so of the 1st array $dists[1][0][0] its picking 1 to 3 from the $index chosen earlier

    $sql = "SELECT * FROM pitches ORDER BY RAND() LIMIT $numtot";// randomise results
    $numtot = rand(9,12); // randomly pick a row between 9 and 12
    $index = $numtot - 9; //change it to 0 to 4 to use the $dists array

    then randomly pick the second array from $dists
    $dist = $dists[$index][array_rand($dists[$index])];
    $dists[1][0][0]


    hope that helps
    Last edited by Arcticwarrio; 06-07-2013 at 08:21 AM.
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • Users who have thanked Arcticwarrio for this post:

    GregoryG (06-08-2013)

  • #3
    New Coder
    Join Date
    Jun 2013
    Posts
    38
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Got it!

    So, in $dist = $dists[$index][array_rand($dists[$index])];, the first use of $dists[$index] (in blue) localizes the result to one of the first-nested arrays, and the second use (in green) makes sure that it is that same array that they array_rand function is picking from!

    Thanks so much. Yesterday this looked like utter redundant madness.


  •  

    Tags for this Thread

    Posting Permissions

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