fields = $this->checkFields($exportFields); $this->data = $data; } /** * 检测导出字段合法性 * @param array $exportFields * @return array|mixed */ public function checkFields($exportFields = []) { // 获取所有数据表字段 $table_name = request('table_name'); $rowTableFields = (new CustomFormField())->getRowTableFieldsByComment($table_name); // 获取交集,则是需要导出的字段 if (!empty($exportFields)) { foreach ($rowTableFields as $key => $item) { if (!in_array($key, $exportFields)) { unset($rowTableFields[$key]); } } } return $rowTableFields; } //数组转集合 public function collection() { return new Collection($this->createData()); } //业务代码 public function createData() { // 表头 $header = array_values($this->fields); $newList = []; foreach ($this->data as $key => $info) { foreach ($this->fields as $k => $v) { $newList[$key][$k] = ($info->$k) ?? ''; } } array_unshift($newList, $header); //插入表头 return $newList; } }