Simple example of replacing a key name in PHP

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.