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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:16:22+00:00 2026-05-19T12:16:22+00:00

I continually find myself running queries on databases and getting multidimensional arrays representing rows

  • 0

I continually find myself running queries on databases and getting multidimensional arrays representing rows back. Unfortunately, the first level index is numeric, which isn’t very informative. I frequently have tables with identity strings in the table, and want to turn the resulting multidimensional array into something that’s associatively indexed by that identity column.

An inelegant self-created custom function to do what I want:

function identity_associate($data, $identity_column='identity'){
    $res = array();
    foreach($data as $single_row){
        $loop_identity = $single_row[$identity_column];
        $res[$loop_identity] = $single_row;
    }
    return $res;
}

Obviously I could use a foreach loop each time I wanted to do this, or use that function to do it for me when I pass the data to it each time, but if there’s a built-in way to do this trivially with native php functions, I’d prefer to use that to make the correction instead. I tried to find something appropriate in the php docs for sorting and arrays, but couldn’t find anything exact, and google wasn’t much help either. Anyone know of something php native or otherwise simpler?

i.e. I want to convert the data that I get when I query the database…

array(
  array(
    'user_id'=>45,
    'identity'=>'bob',
    'name'=>'Bob'
  ),
  array(
    'user_id'=>51, 
    'identity'=>'tchalvak', 
    'name'=>'TchalVak'
  )
);

into something more usable

array(
  'bob'=>array(
    'user_id'=>45, 
    'identity'=>'bob', 
    'name'=>'Bob'
  ), 
  'tchalvak'=>array(
    'user_id'=>51, 
    'identity'=>'tchalvak', 
    'name'=>'TchalVak'
  )
);

as optimized-ly as possible, since it’s something that I would use everywhere all the time.

Edit:

I want to point out that I’m getting the data in that format from the database by calling a built-in function in PDO and it’s returning the equivalent of the first array:

$statement->execute();
return $statement->fetchAll(PDO::FETCH_ASSOC);

E.g. calling the sql below:

$data = query('select name, user_id, identity from users order by identity');

…would get the first array at the top in the question.

  • 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-19T12:16:23+00:00Added an answer on May 19, 2026 at 12:16 pm

    I was thinking about shifting the first element off by applying array mapping the array_unshift() function then pairing the array returned by array_map() and the original array with array_combine(). Here’s a sample, keep in mind it will only work when the identity element is the first element of the sub-arrays:

    <?php
    $arrs = array(
                array(
                    'identity'=>'bob',
                    'user_id'=>45,
                    'name'=>'Bob'
                ),
                array(
                    'identity'=>'tchalvak',
                    'user_id'=>51, 
                    'name'=>'TchalVak'
                )
            );
    
    $identites = array_map( 'array_shift', $arrs );
    $formatted = array_combine( $identites, $arrs );
    print_r( $formatted );
    ?>
    

    If necessary you could craft a helper function to pass to array map if the identity is not the first element.

    <?php
    $arrs = array(
                array(
                    'identity'=>'bob',
                    'user_id'=>45,
                    'name'=>'Bob'
                ),
                array(
                    'identity'=>'tchalvak',
                    'user_id'=>51, 
                    'name'=>'TchalVak'
                )
            );
    
    $identites = array_map( 'array_grab', $arrs );
    $formatted = array_combine( $identites, $arrs );
    print_r( $formatted );
    
    function array_grab( $array ) { return( $array['identity'] ); }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I find myself continually sifting through the net to keep up with Python/Django/web development
I continually get these errors when I try to update tables based on another
One of our developers is continually writing code and putting it into version control
I have a program that continually polls the database for change in value of
When working with my .Net 2.0 code base ReSharper continually recommends applying the latest
I have a lot of assignments where I have to continually update a Makefile
I am looking to optimize a process that runs continually and makes frequent calls
I'm trying to make an SSL call using HTTPWebRequest and its continually failing saying
We have very strange problem, one of our applications is continually querying server by
As I learn more about Computer Science, AI, and Neural Networks, I am continually

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.