I an array of 20 words in PHP. Is there a way to extract a random word from this array that starts with a specific letter?
For example if I want a word starting with B say.
$arr=array('apple','almond','banana','boat','carrot');
Then it will return banana half the time, or boat half the time.
How can I get a random word starting with a given letter from this array?
This should work. After shuffling the array, each word starting with ‘B’ or whichever letter will have a random chance of being first in the shuffled array. Relying on PHP’s shuffle() is probably more efficient and faster than our own implementation.