Export multidimensional array in excel or csv in laravel

℡╲_俬逩灬. 提交于 2019-12-24 08:23:34

问题


Really it's complicated situation for me right now.

Here I have to export multidimensional array data into excel which is looks like below image, but I'm unable to make it possible.

Please help me to export the multidimensional array data to export in excel.

I have an array from relational query like:

$array = [
    0 => [
        'name' => 'Devat Karetha',
    ],
    1 => [
        'name' => 'John Doe',
        'schools' => [
            0 => [
                'school_name' => 'ABC School',
                'school_address' => 'ABC Address',
            ],
            1 => [
                'school_name' => 'XYZ School',
                'school_address' => 'XYZ Address',
            ]
        ],
        'sports' => [
            0 => [
                'id' => 8,
                'sport_type' => [
                    'id' => 2
                    'sport_type_name' => 'ABC Sport'
                ],
            ],
            1 => [
                'id' => 8,
                'sport_type' => [
                    'id' => 3
                    'sport_type_name' => 'ABC Sport'
                ],
            ],
        ],
    ],
    2 => [
        'name' => 'Utkarsh Raval',
        'schools' => [],
        'sports' => [
            0 => [
                'id' => 8,
                'sport_type' => [
                    'id' => 2
                    'sport_type_name' => 'DEKTS Sport'
                ],
            ],
        ],
        'centre' => [
            0 => [
                'id' => 4,
                'centre_name' => 'ABC Centre',
                'centre_mobile' => '9898989898',
            ],
            1 => [
                'id' => 5,
                'centre_name' => 'XYZ Centre',
                'centre_mobile' => '7878787878',
            ],
        ]
    ],
];

Thank You...:)


回答1:


Here I putting my own answer of this question - may be it'll help someone to understand the export functionality with multidimensional array:

    $newArr = [];
    $arr = [];
    foreach($result as $key => $value) {
        $count = [count($value['schools']),count($value['centre']),count($value['sports'])];
        $arrMax = max($count);
        if($arrMax > 0){
            for($i=0; $i < $arrMax; $i++){
                $arrIndex = $i;
                $arr['sr_no'] = '';
                $arr['name'] = '';
                if($arrIndex == '0'){
                    $arr['sr_no'] = $key + 1;
                    $arr['name'] = isset($value['name']) ? $value['name'] : '';
                }
                $arr['school_name'] = isset($value['schools'][$arrIndex]['school_name']) ? $value['schools'][$arrIndex]['school_name'] : '';
                $arr['school_address'] = isset($value['schools'][$arrIndex]['school_address']) ? $value['schools'][$arrIndex]['school_address'] : '';
                $arr['sport_type_name'] = isset($value['sports'][$arrIndex]['sport_type']['sport_type_name']) ? $value['sports'][$arrIndex]['sport_type']['sport_type_name'] : '';
                $arr['centre_name'] = isset($value['centre'][$arrIndex]['centre_name']) ? $value['centre'][$arrIndex]['centre_name'] : '';
                $arr['centre_mobile'] = isset($value['centre'][$arrIndex]['centre_mobile']) ? $value['centre'][$arrIndex]['centre_mobile'] : '';
                $newArr[] = $arr;
            }
        } else {
            $arr['sr_no'] = $key + 1;
            $arr['name'] = isset($value['name']) ? $value['name'] : '';
            $arr['school_name'] = '';
            $arr['school_address'] = '';
            $arr['sport_type_name'] = '';
            $arr['centre_name'] = '';
            $arr['centre_mobile'] = '';
            $newArr[] = $arr;
        }
    }

    /**
     * @param $excel
     */
    \Excel::create('advance-report-'.date('d-m-YTH-i-s'), function($excel) use($newArr) {

        $excel->sheet('Sheet 1', function($sheet) use($newArr) {

            $sheet->fromArray($newArr);
            // Freeze first row
            $sheet->freezeFirstRow();
            // Set height for header
            $sheet->setHeight(1, 30);
            // Modify properties of header
            $sheet->cells('A1:AS1', function($cells) {
                $cells->setBackground('#345fbc');
                $cells->setFontColor('#ffffff');
                $cells->setValignment('center');
            });

            for( $intRowNumber = 1; $intRowNumber <= count($newArr) + 1; $intRowNumber++){
                laravelExcel($sheet, $intRowNumber); //helper function
            }

        });

    })->export('xls');

In Helper File:

/**
 * @param null $sheet
 * @param null $intRowNumber
 */
function laravelExcel($sheet = null, $intRowNumber = null)
{
    try {
        $sheet->setSize('A' . $intRowNumber, 10, 18);
        $sheet->setSize('B' . $intRowNumber, 20, 18);
        $sheet->setSize('C' . $intRowNumber, 20, 18);
        $sheet->setSize('D' . $intRowNumber, 20, 18);
        $sheet->setSize('E' . $intRowNumber, 35, 18);
        $sheet->setSize('F' . $intRowNumber, 15, 18);
        $sheet->setSize('G' . $intRowNumber, 15, 18);
    } catch (\Exception $e) {
        return $e->getMessage();
    }
}


来源:https://stackoverflow.com/questions/50790846/export-multidimensional-array-in-excel-or-csv-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!