When output by default, the database field name is used as the key name of the array for output. However, data with the key name cannot meet the operation in unknown conditions, the following describes a good method to solve this problem. the database operation functions encapsulated in the YII Framework are output by default using the database field name as the array key name, however, sometimes data with a key name cannot meet unknown operations, such as abnormal operations such as exporting database data to EXCEL.
Therefore, we need to parse the database result set. The following is a simple method for this special case:
The code is as follows:
/**
* @ Todo: optimizes the EXCEL table output with the database table field name and key name for YII query and output
* @ Todo replace key name: 0, 1, 2...
* @ Param array $ data
* @ Return array ('Excel _ title' = array (), 'Excel _ ceils '= array ());
*/
Public function excelDataFormat ($ data ){
For ($ I = 0; $ I
$ Each_arr = $ data [$ I];
$ New_arr [] = array_values ($ each_arr); // return all key values
}
$ New_key [] = array_keys ($ data [0]); // return all index values
Return array ('Excel _ title' => $ new_key [0], 'Excel _ ceils '=> $ new_arr );
}
Here, the author writes this method purely for processing EXCEL export. if it is often used in projects, you can change the name again!