vendredi 18 août 2017

How to get random array values, not just keys with php?

I am trying to create little app that randomly selects a number of chords/notes from a scale but I am having trouble with using the array_rand to get those random values from an array. I have an array called $scale that looks like this:

Array
(
  [0] => f#
  [1] => g#
  [2] => a#
  [3] => b
  [4] => c#
  [5] => d#
  [6] => f
)

I also have an array called $chord_amt:

$chord_amt = array(2,3,4,5,6);

I have used the array_rand function to randomly select one item from the array like so:

$selected_chord_amt = $chord_amt[array_rand($chord_amt)];

Now I want to output whatever number of random chords that this function can produce:

$random_chords = array_rand($scale, $selected_chord_amt);

The problem is, if I print this array, instead of seeing the chord/note values it just shows the keys like this example:

Array ( [0] => 3 [1] => 4 ) 

How do I get the actual values, so the above output would look like this?

Array ( [0] => b [1] => c# )

Noob question, I know. Sorry.




Aucun commentaire:

Enregistrer un commentaire