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 8242721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:15:47+00:00 2026-06-07T21:15:47+00:00

Per PDO’s fetchAll() method, I have a two-dimensional array of cities from my database

  • 0

Per PDO’s fetchAll() method, I have a two-dimensional array of cities from my database which I use to generate a dropdown with options, like so:

foreach($option as $city) {
    echo '<option value="'. $city['city_id'] . '">' . $city['city_name'] . '</option>';
}

This is all well and good, but it returns a rather long list of cities from an entire state/province, and so to make selection a little friendlier, I would like to dynamically add categorical <optgroup> “sections” based on their starting letter. The results are already sorted by name, so that part’s taken care of.

I’ve tried using preg_grep() and preg_match() to split the array up into smaller arrays and then echo out an optgroup for each of those, but that doesn’t quite work… optgroup B gets shoved to the bottom of the list after all cities are displayed.

$citiesA = preg_grep('/^A.*/', $option);
$citiesB = preg_grep('/^B.*/', $option);

    echo '<optgroup label="A">';
    foreach($citiesA as $a) {
        echo '<option value="'. $a['city_id'] . '">' . $a['city_name'] . '</option>';
    }
    echo ' </optgroup>';

    echo '<optgroup label="B">';
    foreach($citiesB as $b) {
        echo '<option value="'. $b['city_id'] . '">' . $b['city_name'] . '</option>';
    }
    echo ' </optgroup>';

Any suggestions on how to make this happen are much appreciated.

  • 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-07T21:15:49+00:00Added an answer on June 7, 2026 at 9:15 pm

    How about

    $startl=""; // current Starting letter
    foreach($option as $o)
    {
        if (($c=substr($o['city_name'], 0, 1)) != $startl) // if starting letter changed
        {
            if ($startl) echo '</optgroup>';
            echo '<optgroup label="'. $c .'">'; // create new group
            $startl=$c;
        }
        echo '<option value="'. $o['city_id'] . '">' . $o['city_name'] . '</option>';
    }
    echo '</optgroup>'; // end the last group
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Per a great answer from another question I have begun mounting global resources (css/js/images)
I am fetching an array of floats from my database but the array I
Per a client's request I have written a communication class that inherits from webclient.
As per title, what practise for string comparisons do you use and why?
per my client request I have been requested to return a dataset. basically it's
Per the Agile Development book, I have an Admin MVC that controls how users
Per MSDN , calling Type.GetMethods() stores reflected method information in a MemberInfo cache so
Per an answer to a previous question (answer here: SQL/Database Views in Grails ),
I have a little confusion regarding difference between ORM and PDO? Is PDO a
Question solved... silly mistake i have a little problem with my database update script

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.