Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8475309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:51:30+00:00 2026-06-10T17:51:30+00:00

I an array of 20 words in PHP. Is there a way to extract

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:51:31+00:00Added an answer on June 10, 2026 at 5:51 pm

    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.

    function returnWithFirstLetter($words, $letter) {
        shuffle($words);
        foreach($words as $word)
            if($word[0] == $letter)
                return $word;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is an array of user states stored in the session. This works: <?php
I have a PHP array of IDs of rows to retrieve from a MySQL
Is there a way to logout of a digest authentication done in php. I
Is there a way to programmatically reopen an order in Magento that has already
I have an array in PHP that is simply strings. These strings represent relationships
Is there a way to get the date start/end for each week in PHP
Is there a way to make this code work without a Warning? function myFunction($value,
Possible Duplicate: A PHP regex to extract php functions from code files I have
I've got a simple PHP script which reads specific phrases from a text file
Is there any way in PHP to open a file based on a variable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.