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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:39:59+00:00 2026-05-25T06:39:59+00:00

I’d like to take an array with this structure: array ‘Alabama’ => array 0

  • 0

I’d like to take an array with this structure:

array
  'Alabama' => 
    array
      0 => 
        array
          'id' => string '11' (length=2)
          'region_name' => string 'Alabama' (length=7)
          'city' => string 'Birmingham' (length=10)
      1 => 
        array
          'id' => string '12' (length=2)
          'region_name' => string 'Alabama' (length=7)
          'city' => string 'Huntsville' (length=10)
      2 => 
        array
          'id' => string '13' (length=2)
          'region_name' => string 'Alabama' (length=7)
          'city' => string 'Mobile' (length=6)
      3 => 
        array
          'id' => string '14' (length=2)
          'region_name' => string 'Alabama' (length=7)
          'city' => string 'Montgomery' (length=10)
  'Alaska' => 
    array
      0 => 
        array
          'id' => string '15' (length=2)
          'region_name' => string 'Alaska' (length=6)
          'city' => string 'Anchorage' (length=9)

And create unordered lists in html, like so:

<ul id="A">
    <li class="state">Alabama</li> 
    <li>Birmingham</li>
    <li>Huntsville</li>
    <li>Mobile</li>
    <li>Montgomery</li>
    <li class="state">Alaska</li>
    <li>Anchorage</li>
</ul> 
<ul id="C">
    <li class="state">California</li>
    <li>Bakersfield</li>
    <li>Fresno</li>
    <li>Los Angeles</li>
</ul>
<ul id="D">
    <li class="state">DC</li>
    <li>Washington</li>
</ul>

The idea is an alphabetically ordered and grouped series of unordered lists, which I can show and hide easily using javascript. That part is easy… This part, I’m lost.

I’ve tried a sort of nested foreach loop, but the framework I’m using refused to do it citing OutputEscaper errors, which I believe made sense – I really am not sure how to do this properly.

I’d appreciate any help!

edit: Here’s how the array is initially formatted:

$this->cityGroups = array();
foreach($this->USCities as $city)
{
  $this->cityGroups[$city['region_name']][] = $city;
}
  • 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-05-25T06:40:00+00:00Added an answer on May 25, 2026 at 6:40 am

    This solution should achieve what you’re looking for:

    $lastLetter = "A";
    print( '<ul id="A">' );
    foreach( $myArray as $key => $array ){
        if( strtoupper( substr( $key, 0, 1 ) ) != $lastLetter ){
            print( '</ul>' );
            $lastLetter = strtoupper( substr( $key, 0, 1 ) );
            print( '<ul id="' . $lastLetter . '">' );
        }
        print( '<li class="state">' . $key . '</li>' );
        foreach( $array as $subArr ){
            print( '<li>' . $subArr['city'] . '</li>' );
        }
    }
    print( '</ul>' );
    

    If you need an explanation on any specific part, please let me know.

    Alternate Solution, in the event your starting letter is variable (may or may not be A)

    Change:

    $lastLetter = "A";
    print( '<ul id="A">' );
    

    to:

    $temp = array_keys( $myArray );
    $lastLetter = strtoupper( substr( $temp[0], 0, 1 ) );
    print( '<ul id="' . $lastLetter . '">' );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.