I have a string that looks like this
$genres = "pop,rock,jazz,80s";
i was wondering is it possible to then create a string that randomly selects any of those genres above? but removing the comma?
for example it would create
$newgenre = "pop";
I have a string that looks like this
$genres = "pop,rock,jazz,80s";
i was wondering is it possible to then create a string that randomly selects any of those genres above? but removing the comma?
for example it would create
$newgenre = "pop";
You could use something like this:
$genreArray = explode(',', $genres);
$genre = $genreArray[mt_rand(0, count($genreArray))];
Everyone else is using random selection from an array, try this alternative:
$genreArray = explode(',', $genres);
shuffle($genreArray); //mixes up the array using rand or mt_rand
$genre = $genreArray[0]; // take first element of shuffled array.
You can use the $genreArray = explode(',', $genres) function to put it all into an array.
Then you can generate a random index for the array, using
$randomKey = rand(0, count($genreArray))
And then, all you have to do is take the random genre from the array.
$randomGenre = $genreArray[$randomKey];
You could explode
and get a random value from the array
$genresArray = explode(',',$genres);
$your_values = array_rand ($genresArray,1);
echo $your_values[0];