如何在PHP中转置MYSQL数据库 [英] How to transpose MYSQL db in PHP

查看:88
本文介绍了如何在PHP中转置MYSQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP查询MYSQL数据库以将数据输出到csv文件.

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

我目前能够查询数据库并将数据导出到CSV文件.

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.

代码:

 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

推荐答案

尝试使用此功能:

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>";

返回:

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
        )

)

这篇关于如何在PHP中转置MYSQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆