此代码从$ vec数组中选择2-6个音高。我想回应每个单独的音高,但有趣的是,它给出了数组中音高位置的数值(即:2 5 6而不是D F F#)
$pick = rand(2,6);
$vec = array("C","C#","D","D#","E","F","F#","G","G#","A","A#","B");
$random_keys = array_rand($vec,$pick);
foreach ($random_keys as $pitch){
echo $pitch; echo "<br>";
}
为什么要这样做?我怎样才能得到音高而不是数字呢?
答案 0 :(得分:3)
试试这个:
$pick = rand(2,6);
$vec = array("C","C#","D","D#","E","F","F#","G","G#","A","A#","B");
$random_keys = array_rand($vec, $pick);
foreach ($random_keys as $key) {
echo $vec[$key], '<br />';
}
来自array_rand()
文档:
返回值
如果只选择一个条目,array_rand()将返回随机条目的键。否则,它返回随机条目的键数组。这样做是为了您可以从数组中选择随机键和值。