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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:42:14+00:00 2026-06-03T09:42:14+00:00

Having an associative array like $myarray = array ( ‘user’ => array ( 2

  • 0

Having an associative array like

$myarray = array (
  'user' => array (
      2 => 'john',
      5 => 'done',
      21 => 'foo'
  ),
  'city' => array (
      2 => 'london',
      5 => 'buenos aires',
      21 => 'paris',
  ),
  'age' => array (
      2 => 24,
      5 => 38,
      21 => 16
  )
  ...
);

EDITED
Assuming I don’t know how many keys this array has, nor the keys themselves (they could be whatever) The above is just an example

Is there any elegant way (using built-in functions and not loops) to convert it into

$result = array(
    array(
        'user',
        'city',
        'age'
        ...
    ),
    array(
        'john',
        'london',
        24
        ...
    ),
    array(
        'done',
        'buenos aires',
        38
        ...
    ),
    array(
        'foo',
        'paris',
        16
        ...
    )
);

As a side question: how to obtain this too (in a similar elegant way)

$result = array(
    array(
        'row',
        'user',
        'city',
        'age'
        ...
    ),
    array(
        2,
        'john',
        'london',
        24
        ...
    ),
    array(
        5,
        'done',
        'buenos aires',
        38
        ...
    ),
    array(
        21,
        'foo',
        'paris',
        16
        ...
    )
);
  • 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-03T09:42:16+00:00Added an answer on June 3, 2026 at 9:42 am

    Note that both of these operations are basically the transpose of your original array.

    All of this is strongly adapted from this answer (consider upvoting it!).

    helper

    First we need a helper function, which keeps the row headers as the keys in the associative array:

    function flipArrayKeys($arr) {
        $out = array('row' => array_keys($arr));
        foreach ($arr as $key => $subarr) {
            foreach ($subarr as $subkey => $subvalue) {
                $out[$subkey][] = $subvalue;
            }
        }
        return $out;
    }
    

    flipArrayKeys($myarray) gives:

    array (
      'row' => 
      array (
        0 => 'user',
        1 => 'city',
        2 => 'age',
      ),
      2 => 
      array (
        0 => 'john',
        1 => 'london',
        2 => 24,
      ),
      5 => 
      array (
        0 => 'done',
        1 => 'buenos aires',
        2 => 38,
      ),
      21 => 
      array (
        0 => 'foo',
        1 => 'paris',
        2 => 16,
      ),
    )
    

    part 1

    $result = array_values(flipArrayKeys($myarray));
    

    and now result looks like:

    array (
      0 => 
      array (
        0 => 'user',
        1 => 'city',
        2 => 'age',
      ),
      1 => 
      array (
        0 => 'john',
        1 => 'london',
        2 => 24,
      ),
      2 => 
      array (
        0 => 'done',
        1 => 'buenos aires',
        2 => 38,
      ),
      3 => 
      array (
        0 => 'foo',
        1 => 'paris',
        2 => 16,
      ),
    )
    

    This part can also be done using transpose from this answer:

    $result = transpose($myarray);
    array_unshift($result, array_keys($myarray));
    

    part 2

    function flipArrayWithHeadings($arr) {
        $out = flipArrayKeys($arr);
    
        foreach (array_keys($out) as $key) {
            array_unshift($out[$key],$key);
        }
        return array_values($out);
    }
    

    So flipArrayWithHeadings($myarray) looks like:

    array (
      0 => 
      array (
        0 => 'row',
        1 => 'user',
        2 => 'city',
        3 => 'age',
      ),
      1 => 
      array (
        0 => 2,
        1 => 'john',
        2 => 'london',
        3 => 24,
      ),
      2 => 
      array (
        0 => 5,
        1 => 'done',
        2 => 'buenos aires',
        3 => 38,
      ),
      3 => 
      array (
        0 => 21,
        1 => 'foo',
        2 => 'paris',
        3 => 16,
      ),
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to return groups as an associative array? I'd like to know
I know that I can create an associative array like this: var MyAssocArray =
I'm trying to create an associative array with dynamic data, and having some trouble.
I am having trouble with this association. I need to get an array of
Having volunteered to maintain a stagnant CPAN package (GnuPG) I'd like improve the install
Having trouble with proper regex for RewriteCond RewriteCond %{REQUEST_URI} !^/foo/ Works as expected, that
Having just pulled my hair off because of a difference, I'd like to know
I have an array with the ids of x cakes and another associative array
When declaring an associative array, how do you handle the indentation of the elements
I have an associative array that I might need to access numerically (ie get

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.