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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:53:13+00:00 2026-06-10T16:53:13+00:00

I want to convert this. $data1 = array( array(‘value’ => ‘100.00’, ‘total’ => ’32’),

  • 0

I want to convert this.

$data1 = array(
    array('value' => '100.00', 'total' => '32'),
    array('value' => '10.00', 'total' => '13'),
    array('value' => '200.00', 'total' => '39'),
    array('value' => '190.00', 'total' => '11'),
);

into this

$data2 = array(
    'value' => array(0 => '100.00', 1 => '10.00', 2 => '200.00', 3 => '190.00'),
    'total' => array(0 => '32', 1 => '13', 2 => '39', 3 => '11')
);

I can obviously do this in a roundabout way by iterating over the top array, while appending to a series of arrays, but I figured that there must be a php array function that I don’t know about that can do this more concisely.

http://www.php.net/manual/en/ref.array.php

Values are floats and integers (if it makes any difference), I’ve just added them as strings in the example code because it’s easier to read IMO. Final array order should match the initial order. I’ll award the correct answer to the least LOC providing performance isn’t significantly worse that the ‘long’ version. PHP 5.4.

  • 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-10T16:53:14+00:00Added an answer on June 10, 2026 at 4:53 pm
    <?php // php 5.4 array syntax
    
    $new = array_reduce($data1, function (&$result, $item)
        {
            $result['value'][] = $item['value'];
            $result['total'][] = $item['total']; 
            return $result;
        },
        ['value' => [], 'total' => []]);
    

    ‘value’ and ‘total’ are arbitrary names, so you’re not going to get a one-liner php library function to do this.

    You can refactor this code into a function if you want to…

    function array_rotate($data) {
        $k = array_keys($data[0]);
        return array_reduce($data, function (&$r, $i) use ($k) {
            $r[$k[0]][] = $i[$k[0]];
            $r[$k[1]][] = $i[$k[1]]; 
            return $r;
        }, [$k[0] => [], $k[1] => []]);
    }
    

    I generalised @matthew’s code, this allows an arbitrary number of keys (instead of 2):

    function array_rotate2($data) {
        return array_combine(array_keys($data[0]), 
            array_map(function ($field) use ($data) {
                  return array_map(function($row) use ($field) { return $row[$field]; }, $data);
                }, array_keys($data[0])));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is Pandas dataframe I want to convert 1D data into 2D array form
I want to convert this linear loop into a concurrent one: for(Item item :
i want to convert this array Array ( [2] => Array ( ) [4]
i want to convert this string into DateTime. Tue Aug 19 15:05:05 +0000 2008
i have an array and i want to convert this array in comma seprated
I have an array of custom objects. MyCustomArr[]. I want to convert this to
I have a large data set and i want to convert it into array.
I want to convert a string into a byte array. (Yes) I have seen
i want to convert some Cookie Data into Array . example $cook = lg=en;
Initially I want to convert this uint8_t array to a char array in c.

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.