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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:08:19+00:00 2026-05-13T18:08:19+00:00

Whats the easiest way to invert a multidimensional array. By invert I mean similar

  • 0

Whats the easiest way to invert a multidimensional array. By invert I mean similar to array_flip.

e.g

[0][5][var_name] = data
[0][3][var_name2] = data2
[1][var_name3] = data3
[0][1][4][var_name4] = data4

inverted would be:

[var_name][0][5] = data
[var_name2][0][3] = data2
[var_name3][1] = data3
[var_name4][0][1][4] = data4

Any ideas?

  • 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-13T18:08:19+00:00Added an answer on May 13, 2026 at 6:08 pm

    You could store a list of the keys of all ancestors and when you hit a “leaf” use this list to create the “flipped” version.

    <?php
    $data = array(
      0=>array(
        5=>array('var_name' => 'data'),
        3=>array('var_name2' => 'data2'),
        1=>array(4=>array('var_name4'=>'data4'))
      ),
      1=>array('var_name3'=>'data3')
    );
    
    $result = array();
    foo($data, $result);
    
    ($result['var_name'][0][5] === 'data') or die('1');
    ($result['var_name2'][0][3] === 'data2') or die('2');
    ($result['var_name3'][1] === 'data3') or die('3');
    ($result['var_name4'][0][1][4] === 'data4') or die('4');
    echo 'ok';
    
    
    function foo(array $a, array &$target, $stack=array()) {
      foreach($a as $key=>$value) {
        if ( is_array($value) ) {
          array_push($stack, $key);
          foo($value, $target, $stack);
          array_pop($stack);
        }
        else {
          $target[$key] = array();
          $tmp = &$target[$key];
          foreach( $stack as $s ) { // now it's not so stack-ish anymore :-S
            $tmp[$s] = array();
            $tmp = &$tmp[$s];
          }
          $tmp = $value;
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whats the easiest way to take a JSON or Array object and convert it
Whats the easiest way to find out what programming language an application was written
Whats the easiest way in javascript to replace ABC, DEF, GHI with XYZ in
In TFS whats the easiest way of linking a backlog item to a large
Whats the best/easiest way to obtain a count of items within an IEnumerable collection
Whats the best/easiest way to test for administrative rights in a PowerShell script? I
How do I add multiple strings to a string ? Whats the easiest way
Whats the easiest way to determine which Java class is compiled with the wrong
Whats the easiest way to copy a visual studio project between distinct solutions? can
What is the easiest and most efficient way to insert data from a DataTable

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.