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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:07:25+00:00 2026-05-23T13:07:25+00:00

I have a multidimensional array in which the first level keys needs to be

  • 0

I have a multidimensional array in which the first level keys needs to be the first line of data in .csv string.

The rows of indexed elements need to be transpose and converted into subsequent lines of csv text.

The output will be identical to a .csv file’s contents, but I don’t want a file to be created, just as a .csv string.

Sample data:

$data = [
     'dates' => ['2010-01-02', '2011-02-03', '2011-02-04'],
     'type1' => ['data1', 'data2', 'data3'],
     'type2' => ['data4', 'data5', 'data6']
];

Desired output string:

dates,type1,type2
2010-01-02,data1,data4
2011-02-03,data2,data5
2011-02-04,data3,data6
  • 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-23T13:07:26+00:00Added an answer on May 23, 2026 at 1:07 pm

    This is much more verbose than you’ll likely ever need, but it allows you to have an array of n elements. zip is based off of the Python function of the same name. I did not write it.

    $data = ($data);
    $r = (call_user_func_array('zip', $data));
    
    $fin = "";
    $fin .= implode(',',array_keys($data)).PHP_EOL;
    foreach($r as $arr)
    {
        $fin .= implode(',',$arr).PHP_EOL;
    }
    
    // $fin now holds all the data. Do something with it and you're finished!
    
    function zip() {
        $args = func_get_args();
        $zipped = array();
        $n = count($args);
        for ($i=0; $i<$n; ++$i) {
            reset($args[$i]);
        }
        while ($n) {
            $tmp = array();
            for ($i=0; $i<$n; ++$i) {
                if (key($args[$i]) === null) {
                    break 2;
                }
                $tmp[] = current($args[$i]);
                next($args[$i]);
            }
            $zipped[] = $tmp;
        }
        return $zipped;
    }
    

    (check out the conversation in the comments. It’s really relevant.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multidimensional array which I would like to sort by two factors:
I have a multidimensional array which is the result of a JOIN statement. And
I have a method which returns a array of ByteArray: public byte[][] Draw(ImageFormat imageFormat,
I continually find myself running queries on databases and getting multidimensional arrays representing rows
I'm having some variables in my Hash of multidimensional arrays disappear as soon as
I've made a function (posted at the bottom), which will look at a multi-dimensional
I have a small C# library with a static class that holds a very
I have an assignment for a class that is to be done in C#.
Cant really find any useful information on this through Google so hope someone here
Is it possible, in Java, when you hover over a single button, to make

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.