0

This code picks 2-6 pitches from the $vec array. I'd like to echo out each individual pitch, but interestingly enough, it gives me the numeric values of the placement of the pitch in the array (ie: 2 5 6 instead of 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>";
  }

Why is it doing this and how can I get the pitches instead of the numbers?

4

1 回答 1

3

Try this:

$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 />';
}

From array_rand() documentation:

Return value

If you are picking only one entry, array_rand() returns the key for a random entry. Otherwise, it returns an array of keys for the random entries. This is done so that you can pick random keys as well as values out of the array.

于 2013-06-09T04:55:38.973 回答