将array_rand函数与多维数组一起使用

时间:2022-07-04 01:49:15

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

在这个帖子中,Pudge601非常善于为我的问题提供解决方案:Php / MySQL随机数据(音高)序列

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

通过将静态值替换为随机值,我弄清楚while循环是如何工作的。但是,我仍然试图理解这一行:

$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!

它似乎与这里的描述不对应:http://php.net/manual/en/function.array-rand.php在此上下文中使用多维数组时,语法可能不同?无论如何,我只是没有得到它。如果有人可以帮我翻译成“英语”,我会感激不尽!

2 个解决方案

#1


2  

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.

如果只选择一个条目,array_rand()将返回随机条目的键。

#2


0  

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

后来在这个讨论中解决了同样的问题: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.

答案:在$ dist = $ dists [$ index] [array_rand($ dists [$ index])];中,首次使用$ dists [$ index]将结果本地化为第一个嵌套数组之一,第二个use确保它是array_rand函数从中拾取的相同数组。

#1


2  

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.

如果只选择一个条目,array_rand()将返回随机条目的键。

#2


0  

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

后来在这个讨论中解决了同样的问题: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.

答案:在$ dist = $ dists [$ index] [array_rand($ dists [$ index])];中,首次使用$ dists [$ index]将结果本地化为第一个嵌套数组之一,第二个use确保它是array_rand函数从中拾取的相同数组。