An example of an easy way to replace key names in PHP is explained in detail

  • 2020-12-05 17:08:45
  • OfStack

The database operation function encapsulated in YII framework outputs the database field name as the key name of the array by default, but sometimes the data with the key name cannot satisfy the operation under unknown circumstances, such as: the database data is exported to EXCEL and other relatively abnormal operations.

Therefore, the database result set needs to be parsed. Here is a simple method for this special case:
 
/** 
* @todo  for YII  The query output is optimized with the database table field name key name EXCEL Form the output  
* @todo  Replacement key name 0 , 1 , 2... 
* @param array $data 
* @return array('excel_title'=array(),'excel_ceils'=array()); 
*/ 
public function excelDataFormat($data){ 
for ($i=0;$i<count($data);$i++){ 
$each_arr=$data[$i]; 
$new_arr[]=array_values($each_arr); // Returns all key values  
} 
$new_key[]=array_keys($data[0]); // Returns all index values  
return array('excel_title'=>$new_key[0],'excel_ceils'=>$new_arr); 
} 

The author wrote this method here for the sole purpose of handling the EXCEL export. If you use it frequently in your project, you can change the name!

Related articles: