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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:24:08+00:00 2026-05-23T21:24:08+00:00

example: Array ( [0] => Array ( [‘user_id’] => 4567 [‘user_name’] => ‘John Doe’

  • 0

example:

Array
(
 [0] => Array
    (
        ['user_id'] => 4567
        ['user_name'] => 'John Doe'
        ['city'] => 'NY'
        // and so on
    )


 [1] => Array
    (
        ['user_id'] => 3732
        ['user_name'] => 'Janet Smith'
        ['city'] => 'LA'
        // and so on
    )
 // and here many more rows

but I need this:

Array
(
 [4567] => Array
    (
        ['user_id'] => 4567
        ['user_name'] => 'John Doe'
        ['city'] => 'NY'
        // and so on
    )


 [3732] => Array
    (
        ['user_id'] => 3732
        ['user_name'] => 'Janet Smith'
        ['city'] => 'LA'
        // and so on
    )
 // and here many more rows

So I can acces given row by it’s id.
The only one way I know is to loop this whole array that can be huge:

 foreach($array_from_db as $row) {
   $idexed_array_from_db[$row['user_id']] = $row;
 }

Are there other ways to achieve this result ?

  • 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-23T21:24:09+00:00Added an answer on May 23, 2026 at 9:24 pm

    I don’t think PDO can do this directly. There are other wrappers which can, such as ADODb with GetAssoc

      $array=$db->GetAssoc("select user_id,user_name,city from user");
    

    However, any wrapper is simply retrieving the result and building the array just as you describe in your question. The only difference is that your suggestion fetches the entire resultset into an array first, then generates your associative array from that.

    If you’re using PDO, your then a more memory efficient approach would be to read the results row-by-row and build the array that way.

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

Sidebar

Related Questions

Here is an example array: $foo = array( 'employer' => array( 'name' => 'Foobar
Per the example array at the very bottom, i want to be able to
example: I want to see if array[5] holds a value or is empty.
I was wondering if anyone had an example of what an array would look
Is it possible in c# to initialize an array in, for example, subindex 1?
I have two arrays of animals (for example). $array = array( array( 'id' =>
What's the best way to initialize an array in PowerShell? For example, the code
How can I find the largest increasing (non-contiguous) subset of an array? For example,
I have a DependencyObject in C# that is used in an array. Example shown
It is often useful to implement algorithms using nested array operations. For example, 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.