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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:41:14+00:00 2026-06-07T13:41:14+00:00

I am getting information from a database that I have no control over. The

  • 0

I am getting information from a database that I have no control over. The value of “state” is a user entered (and properly cleaned) value, but may be the written out state name or the two letter postal abbreviation. I can easily build an associative array of states and abbreviations. But I was wondering if there is a way, PHP, to determine if a value is in the array of states/abbrev as a key OR a value. So, if you enter “CA” it sees it is a valid two letter key and returns it. If it sees “XY” is NOT a valid key then it retuns a default “OTHER” key (ZZ), but if the user entered input is “New York” it will see it is a valid value and return the associated key, “NY”?

  • 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-07T13:41:17+00:00Added an answer on June 7, 2026 at 1:41 pm
    $userInput; // Your user's input, processed using regex for capitals, etc to match DB values for the strings of the states.
    // Otherwise, do your comparisons in the conditions within the loop to control for mismatching capitals, etc.
    
    $output = false;
    
    foreach ($stateArray as $abbreviation => $full) // Variable $stateArray is your list of Abbreviation => State Name pairs.
    {
        if ($userInput == $abbreviation || $userInput == $full) // Use (strtolower($userInput) == strtolower($abbreviation) || strtolower($userInput) == strtolower($full)) to change all the comparison values to lowercase.
        // This is one example of processing the strings in a way to ensure some flexibility in the user input.
        // However, whatever processing you need to do is determined by your needs.
        {
            $output = array($abbreviation => $full); // If you want a key => value pair, use this.
            $output = $abbreviation; // If you only want the key, use this instead.
            break;
        }
    }
    
    if ($output === false)
    {
        $output = array("ZZ" => "OTHER"); // If you want a key => value pair, use this.
        $output = "ZZ"; // If you only want the key, use this instead.
    }
    

    Edit: I’ve changed the loop so that it checks user input against abbreviation and full state name in one condition instead of having them separate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a combo box that is currently getting its information from a database
I have the following ColdFusion code that is getting information from a database and
I have an iPhone application which basically is getting information from an API (in
I am getting a large text file of updated information from a customer that
Okay, I am developing an Application which requires getting sensitive information from a database
We have a database that contains information about time booked to projects. We have
I have a web-form that is getting customer information. The form is processed using
I have a form that will be populated by a bunch of information from
I have been following several tutorials that have made me succeed in getting information
I'm working on a project that involved getting information from two different servers. What

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.