Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

In this thread, Pudge601 was so kind as to offer a solution to my problem: Php/MySQL random data (musical pitch) sequences

By substituting static values for random ones, I figured out how the while loop works. However, I am still trying to understand this line:

$dist = $dists[$index][array_rand($dists[$index])];

I can understand it when I substitute (for example)

$dist = $dists[$index][0]

Which retrieves the first array value from one of the nested arrays. BUT, I do not see how this portion:

[array_rand($dists[$index])];

Produces one of the desired values.

It does not seem to corresponds to the description here: http://php.net/manual/en/function.array-rand.php Perhaps the syntax is different when using the multidimensional array in this context? In any event, I'm just not getting it. If someone could help me make the translation to 'english', I'd be thankful!

share|improve this question
    
If you can understand $dists[$index][0], why not $dists[$index][array_rand($dists[$index])]? In this array_rand($dists[$index]) returns a number, for example 0, so just substitute that mentally and it's the same. – deceze Jun 8 '13 at 7:24

The code should be read as:

$arr = $dists[$index]; // select array from $dists element at index $index
$key = array_rand($arr); // get key of a random element
$dist = $arr[$key]; // get element value

From the documentation:

If you are picking only one entry, array_rand() returns the key for a random entry.

share|improve this answer
    
Thanks, but I've been looking at this and it still just doesn't click. I follow that the array_rand function is only picking one entry. What I'm just not seeing is how it is selecting one of the arrays on the second level of nesting. Eg: "array(4,3,3),array(3,4,3),array(3,3,4))," It seems to me like $dists[$index] should refer to the first level of nesting in both usages - that I think is what is confusing me. – Gregory Tippett Jun 7 '13 at 4:25
    
$dists[$index] is itself an array; not sure if that's causing the confusion? – Ja͢ck Jun 7 '13 at 4:26
    
hmmm. Basically, I can feel that there is some gap in my knowledge here that would enable me to understand how that structure works - but I have no idea what the terms are to describe that gap. If I did I'd know! Thanks for trying. I'll append something later on if I can articulate what the problem is more clearly. – Gregory Tippett Jun 7 '13 at 5:23

This same question was later resolved in this discussion: http://www.codingforums.com/showthread.php?t=296450

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.