Saturday, 8 June 2013

php array_rand function with foreach

php array_rand function with foreach

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?

No comments:

Post a Comment