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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:12:44+00:00 2026-05-27T01:12:44+00:00

I have a simple table with 2 colums: ID and Name . It looks

  • 0

I have a simple table with 2 colums: ID and Name. It looks like this:

License_ID      License_Name
   5             xxx
   8             yyy
   13            zzz

I want to write this table as an array, and I want to be able to find the license_id when license_name is provided. How can I write this array?

Edit: I can write the array the same as the answer nickb provided,

$array = array(
    'xxx' => 5,
    'yyy' => 8,
    'zzz' => 13
);

but I want to have the license_id and license_name as key instead of using value of license_name as key.

  • 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-27T01:12:45+00:00Added an answer on May 27, 2026 at 1:12 am

    Form an array with keys corresponding to the license name and values corresponding to the license ID, like so:

    $sql = 'SELECT * FROM table'; // Optionally add a WHERE clause
    $result = mysql_query( $sql);
    $array = array();
    while( $row = mysql_fetch_array( $result))
    {
        // Form the array
        $array[ $row['License_Name'] ] = $row['License_ID'];
    }
    mysql_free_result( $result);
    

    To omit the table and use a global variable, use the same technique:

    $array = array(
        'xxx' => 5,
        'yyy' => 8,
        'zzz' => 13
    );
    

    Now, to lookup the license ID, all you need is:

    $license_id = 8;
    $id = $array[ $license_id ]; // $id = 'yyy';
    

    Edit: Here’s an alternative way to represent the way in a more clear manner:

    $array = array(
        array(
            'license_id' => 5,
            'license_name' => 'xxx'
        ),
        array(
            'license_id' => 8,
            'license_name' => 'yyy'
        ),
        array(
            'license_id' => 13,
            'license_name' => 'zzz'
        )
    );
    

    Then, to find the license_id when license_name is provided, loop to find your result:

    $license_name = 'zzz';
    foreach( $array as $entry)
    {
        if( $entry['license_name'] == $license_name)
        {
            echo 'Found ID of ' . $license_name . ' - ' . $entry['license_id'];
            break;
        }
    }
    

    OR, and even more direct way to prevent looping is to modify the above technique to have each sub-array contain the license_name as its key, like so:

    $array = array(
        'xxx' => array(
            'license_id' => 5,
            'license_name' => 'xxx'
        ),
        'yyy' => array(
            'license_id' => 8,
            'license_name' => 'yyy'
        ),
        'zzz' => array(
            'license_id' => 13,
            'license_name' => 'zzz'
        )
    );
    

    Now, to find the license_id, you can do it directly once you know the license_name:

    $license_name = 'zzz';
    $license_id = $array[ $license_name ]['license_id'];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple table called genre , which looks like this: GENRE
I have an in-memory table that might looks something like this: Favorite# Name Profession
I have a table which looks like this: Name | Quantity | Unit -------+-------------+----------
I have a table like this: Table name: PRODUCT ID PRODUCTID PRODUCTNAME 1 D100
Imagine I have this simple table: Table Name: Table1 Columns: Col1 NUMBER (Primary Key)
I have a table whose header looks like this (I've simplified it): id, a1,
I have a simple table in Sybase, let's say it looks as follows: CREATE
I want to have a database table that keeps data with revision history (like
My table structure (in SQL Server) looks something like this: (D1 is more recent
I have information in a table like so: id name recommender 1 daniel steve

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.