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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:10:41+00:00 2026-05-27T17:10:41+00:00

Trying to convert a "grouped" multidimensional array to a "segmented" multidimensional array. Not sure

  • 0

Trying to convert a "grouped" multidimensional array to a "segmented" multidimensional array. Not sure how to explain it exactly so hopefully these print_r results explain it better.

Current Array

Array
(
    [qty] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
        )

    [item_id] => Array
        (
            [0] => RR332
            [1] => FF586
            [2] => QW865
        )

    [item_name] => Array
        (
            [0] => Widget 1
            [1] => Widget 2
            [2] => Widget 3
        )

    [price] => Array
        (
            [0] => 1.00
            [1] => 1.50
            [2] => 1.50
        )

)

Wanted Array

Array
(
    [0] => Array
        (
            [qty] => 1
            [item_id] => RR332
            [item_name] => Widget 1
            [price] => 1.00
        )

    [1] => Array
        (
            [qty] => 1
            [item_id] => FF586
            [item_name] => Widget 2
            [price] => 1.50
        )

    [2] => Array
        (
            [qty] => 1
            [item_id] => QW865
            [item_name] => Widget 3
            [price] => 1.50
        )

)

This seems like it should be a simple task but I haven’t been able to crack it yet.

I would preferably like to loop through the Current Array to create the Wanted Array if possible.

  • 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-27T17:10:41+00:00Added an answer on May 27, 2026 at 5:10 pm

    I think this will do the job for you, though I don’t have a good data set to test against.

    $wanted_array = array();
    // Iterate over the outer array to get the text keys & inner arrays
    foreach ($current_array as $txtkey=>$arr) {
      // Iterate over each inner array to get the numeric key
      foreach ($arr as $numkey=>$val) {
        // Set a numeric key pointing to a new array 
        // onto the new outer array if it isn't already there
        if (!isset($wanted_array[$numkey])) $wanted_array[$numkey] = array();
    
        // Swap the keys of the outer and inner arrays.
        $wanted_array[$numkey][$txtkey] = $val;
      }
    }
    

    Update: Using test array from @hakre’s answer, here it is in practice:

    $current_array = array(
      'qty' => array(
        0 => 1,
        1 => 1,
        2 => 1
      ),
      'item_id' => array(
        0 => 'RR332',
        1 => 'FF586',
        2 => 'QW865'
      ),
      'item_name' => array(
        0 => 'Widget 1',
        1 => 'Widget 2',
        2 => 'Widget 3'
      ),
      'price' => array(
        0 => '1.00',
        1 => '1.50',
        2 => '1.50'
      )
    );
    // Output:
    Array
    (
        [0] => Array
            (
                [qty] => 1
                [item_id] => RR332
                [item_name] => Widget 1
                [price] => 1.00
            )
    
        [1] => Array
            (
                [qty] => 1
                [item_id] => FF586
                [item_name] => Widget 2
                [price] => 1.50
            )
    
        [2] => Array
            (
                [qty] => 1
                [item_id] => QW865
                [item_name] => Widget 3
                [price] => 1.50
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to convert a string to NSURL and this is not happening. barcodeTextLabel.text =
I'm trying to convert numbers into english words, for example 1234 would become: "
I am trying to convert a C++ struct to C but keep getting "undeclared
I am trying to convert a standard std::string into an NSString but I'm not
i´m using subsonic 3 trying convert a SQL2008 project to MySQL. when the projects
How to do an update in LINQ to Objects. Trying convert SQL to Linq
Trying to convert the following but am having difficulty. Can anyone see where I'm
Trying to convert this c code into MIPS and run it in SPIM. int
I've been trying to convert SVG images to PNG using C#, without having to
I'm trying to convert old QuickTime framework code to the 64-bit Cocoa-based QTKit on

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.