douyan4243 2016-08-28 13:26
浏览 47

按另一个数组对多维数组进行排序,并以相同的顺序完成缺少的键

I'm trying to export data from leads table to excel using PHP/Laravel, the leads table caמ be customized to show specific columns and the export columns should be same as the table columns. the problem is - if column not exists in a specific row the column not shown empty and the 'xls' file is exported really messy... my code looks like this:

public function excelExport(Request $request, $client_id=null)
{

    $client_id = is_null($client_id) ? \Auth::client()->id : $client_id;

    if(is_null($this->client_users)){
        $this->client_users = $this->clientsController->listClientUsers($client_id);
    }

    $columns = $this->account_settings->getColumnsDef($client_id);
    $query = Lead::select(array_keys($columns))->where('client_id',strval($client_id))->where('deleted',0);
    $query = $this->setQueryDates($request,$query,$client_id);
    $query = $this->leadsFiltersController->setExportQuery($request,$query);
    $arr = $query->get()->toArray();

    Excel::create('leads' , function($excel) use ($arr,$columns){

        $excel->sheet('Leads' , function($sheet) use ($arr,$columns){

            $rows = [];
            $count = 0;

            foreach($arr as $lead){
                $row = $this->setExportRowData($lead,$count,$columns);
                $rows[] = $row;
                $count++;
            }
            $sheet->fromArray($rows);

        });

    })->export('xls');


 private function setExportRowData($lead,$count,$columns,$order = true)
{
    $row = [];
    $order = null;
    foreach($lead as $k => $v) {
if (is_array($v)) {

            switch (strtolower($k)) {

                case "ga_details":
                    if (count($v) > 2) {
                        foreach ($v as $key => $ga_detail) {
                            if ($key != "realTime_data" && $key != "uacid") {
                                $row[$key] = $ga_detail;
                            }
                        }
                    }
                    break;

                case "status":
                    if (isset($v['name']) && count($v['name'])) {
                        $row['status'] = $v['name'];
                    } else {
                        $row['status'] = "no status";
                    }
                    break;

                case "owner":
                    if (isset($v['owner_id']) && count($v)) {
                        $row['owner'] = $this->getClientOwnerUserName($this->client_users, $v['owner_id']);
                    } else {
                        $row['owner'] = "no owner";
                    }
                    break;

                case "prediction":
                    if (isset($v['score']) && count($v)) {
                        $row['prediction'] = $v['score'];
                    } else {
                        $row['prediction'] = "no prediction";
                    }
                    break;

                case "the_lead":
                    foreach ($v as $key => $lead_detail) {
                        $row[$key] = $lead_detail;
                    }
                    break;
                case "quality":
                    if (isset($v['name'])) {
                        $row['quality'] = $v['name'];
                    } else {
                        $row['quality'] = "no quality";
                    }
                    break;

                case "feeds":
                    if (isset($v['feeds']) && count($v['feeds'])) {
                        $row['feeds'] = array_pop($v['feeds'])['message'];
                    }
                    break;

                default :
                    $row[$k] = $v;
                    break;

            }

        } else {
            if ($k != "_id") {
                $row[$k] = $v;
            }

        }


    }

    return $order == true ? sortArrayByArrayValues($this->order,$row) : $row;
}

sortArrayByArrayValues function looks like this:

function sortArrayByArrayValues(Array $array, Array $orderArray)
{
    $rtn = [];
    foreach($array as $arr){
        if(isset($orderArray[$arr])){
            $rtn[$arr] = $orderArray[$arr];
        }else{
            $rtn[$arr] = '';
        }
    }
    return $rtn;
}

I really have no clue how to solve it, appreciate any help!!! :)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 mmocr的训练错误,结果全为0
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀