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

  • Home
  • SEARCH
  • 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 8931413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:06:40+00:00 2026-06-15T09:06:40+00:00

I have an array with terms and definitions: $myArray = array(apple => Fruit that

  • 0

I have an array with terms and definitions:

$myArray = array("apple" => "Fruit that grows ...", "car" => "Vehicle on four...");  

How do I determine if I have words starting with a particular letter in $myArray, say, “c” for car and add a class to corresponding letter in my ABC list that is generated via a loop:

foreach(range('A','Z') as $i) {
    echo '<div>'. $i .'</div>';
}

UPDATE:

Expected output:

<div class="match">A</div>
<div>B</div>
<div class="match">C</div>
  • 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-15T09:06:41+00:00Added an answer on June 15, 2026 at 9:06 am

    You need to compare whether the character you are about to output does exist as a first character in the array keys. Compared with all the other working solutions, which iterate over the whole data array over and over again, using a little preparation should make it easier and more understandable what happens.

    $myArray = array("apple" => "Fruit that grows ...", "car" => "Vehicle on four...", "cat" => "Meow");
    
    // Prepare array keys
    $keys = array_keys($myArray);
    array_walk($keys, function(&$key){$key=mb_strtoupper(mb_substr($key,0,1));});
    array_unique($keys);
    
    foreach (range('A','Z') as $char) {
        if (in_array($char, $keys)) {
            echo "<div class='match'>".$char."</div>";
        } else {
            echo "<div>".$char."</div>";
        }
    }
    

    Output:

    <div class='match'>A</div><div>B</div><div class='match'>C</div><div>D</div><div>E</div><div>F</div> ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have an array of strings such as: var fruit = [apple,peach,pear,plum];
I have an array of terms that I would like to match using the
I would like to have a static char array member initialized in terms of
I have a PHP array of strings: ie: Big green car parked outside..etc I
I'm starting from the Apple-spupplied iOS OpenGL game template in Xcode and I have
I have an array, called x, whose size is 6*sizeof(float). I'm aware that declaring:
Hi all i have a array like string[,] terms; if i want to put
I have an array of search terms, and a setence. I need to test
I have an array of data that is returned as a JSON response. The
Let's say I have an array of n elements(numbers or words), I want to

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.