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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:47:09+00:00 2026-06-13T06:47:09+00:00

I am using PHP to query a MYSQL db to output data to a

  • 0

I am using PHP to query a MYSQL db to output data to a csv file.

I am currently able to query the db and export the data to the CSV file.

However i am unable to transpose the data so that the columns are rows, and rows are columns.

CODE:

 function transpose($array) {
             if (!is_array($array) || empty($array)) {
             return array();

             else {
                foreach ($array as $row_key => $row) {
                    if (is_array($row) && !empty($row)) { //check to see if there is a                  second dimension

                foreach ($row as $column_key => $element) {
                       $transposed_array[$column_key][$row_key] = $element;

        else {
               $transposed_array[0][$row_key] = $row;

                   return $transposed_array;

    }
}

    exportMysqlToCsv($tablename,$tokenmain, $id);
    function exportMysqlToCsv($tablename,$tokenmain, $id, $filename = 'Results.csv'){
        $sql_query = "select * from $tablename";

        // Gets the data from the database
        $result = mysql_query($sql_query);

        $f = fopen('php://temp', 'wt');
        $first = true;

        while ($row = mysql_fetch_assoc($result)) {

            if ($first) {

                fputcsv($f, array_keys($row));

                $first = false;
     }

      fputcsv($f, $row);

        } // end while

        $size = ftell($f);
        rewind($f);
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-Length: $size");

     // Output to browser with appropriate mime type, you choose ;)
        header("Content-type: text/x-csv");
        header("Content-type: text/csv");
        header("Content-type: application/csv");
        header("Content-Disposition: attachment; filename=$filename");

        fpassthru($f);

        exit;
    }

Somehow, those two functions should be intertwined to give me the output I need.

Any help would be greatly appreciated. thanks!
-Merica

  • 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-13T06:47:10+00:00Added an answer on June 13, 2026 at 6:47 am

    Try this function:

    function array_transpose($array, $selectKey = false) {
        if (!is_array($array)) return false;
        $return = array();
        foreach($array as $key => $value) {
            if (!is_array($value)) return $array;
            if ($selectKey) {
                if (isset($value[$selectKey])) $return[] = $value[$selectKey];
            } else {
                foreach ($value as $key2 => $value2) {
                    $return[$key2][$key] = $value2;
                }
            }
        }
        return $return;
    }
    
    
    $fruits = array(
        array('id' => 1, 'name' => 'Apple', 'color' => 'Red'),
        array('id' => 2, 'name' => 'Orange', 'color' => 'Orange'),
        array('id' => 3, 'name' => 'Mango', 'color' => 'Yellow')
    );
    echo "<pre>";
    print_r(array_transpose($fruits));
    echo "</pre>";
    

    Returns:

    Array
    (
        [id] => Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
            )
    
        [name] => Array
            (
                [0] => Apple
                [1] => Orange
                [2] => Mango
            )
    
        [color] => Array
            (
                [0] => Red
                [1] => Orange
                [2] => Yellow
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple query (in a mySQL view) that php is using to
I am using a PHP mySQL query. Struggling with a Joins. mysql_query(SELECT * FROM
i am trying to pass date to mysql query using php, but i am
I am trying to run a mysql insert query using php exec with parameters.
I am using PHP/Mongo to query a data set like the following: { description:
I'm using following code but cannot return data from MySQL. This is the output:
I'm importing data from a CSV file that comes from excel, but i can't
I am importing data from outside into my database MYSQL using PHP Scripts. Encoding
original question: <select> dropdown output my MYSQL data properly using JS function I tried
i retrieved data from mysql tables in json using php i.e $table_first = 'recipe';

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.