How to get random array values, not just keys with php? -
i trying create little app randomly selects number of chords/notes scale having trouble using array_rand random values array. have array called $scale looks this:
array ( [0] => f# [1] => g# [2] => a# [3] => b [4] => c# [5] => d# [6] => f ) i have array called $chord_amt:
$chord_amt = array(2,3,4,5,6); i have used array_rand function randomly select 1 item array so:
$selected_chord_amt = $chord_amt[array_rand($chord_amt)]; now want output whatever number of random chords function can produce:
$random_chords = array_rand($scale, $selected_chord_amt); the problem is, if print array, instead of seeing chord/note values shows keys example:
array ( [0] => 3 [1] => 4 ) how actual values, above output this?
array ( [0] => b [1] => c# ) noob question, know. sorry.
i'd suggest using different approach, actually. using array_rand on array
$chord_amt = array(2,3,4,5,6); isn't needed generate random number between 2 , 6. php has rand function that.
consider following instead:
// shuffle list of chords shuffle($scale); // take slice of random length between 2 , 6 $random_chords = array_slice($scale, 0, rand(2, 6));
Comments
Post a Comment