Php 使用maatwebsite在laravel中导出带有动态数组的excel
我想在Laravel 5.7版中导出Excel。我使用的是Maatwebsite Laravel Excel 3.1版软件包。我有一系列这样的个人信息:Php 使用maatwebsite在laravel中导出带有动态数组的excel,php,laravel-5,maatwebsite-excel,Php,Laravel 5,Maatwebsite Excel,我想在Laravel 5.7版中导出Excel。我使用的是Maatwebsite Laravel Excel 3.1版软件包。我有一系列这样的个人信息: $persons = { "person 1": [ { "answer1": "shokouh" }, { "answer2": "dareshiri" },
$persons = {
"person 1": [
{
"answer1": "shokouh"
},
{
"answer2": "dareshiri"
},
{
"answerN": "answer N"
}
],
...
"person M ": [
{
"answer1": "sarah"
},
{
"answer2": "smith"
},
{
"answerN": "answer N"
}
]
}
我想将其导出到下面的excel中:
Person 1 Answer 1 Answer 2 … Answer N
Person M Answer M2 Answer M2 … Answer MN
这是我在控制器中的导出功能:
public function export(Request $request, Tournament $tournament) {
...
$persons;
return Excel::download(new ParticipantExport($persons), 'personInformation.xlsx');
}
这是我在app\Export\participanexport中的participanexport:
class participantExport implements FromArray
{
protected $invoices;
public function __construct(array $invoices)
{
$this->invoices = $invoices;
}
public function array(): array
{
return $this->invoices;
}
}
任何人都能帮我吗?我解决了这个问题如下: 在Controller.php中:
$rows = array();
foreach ($persons as $slug => $person) {
$row = array();
array_push($row, $slug);
foreach ($person as $key => $value) {
array_push($row, $persons[$slug][$key]['answer']);
}
array_push($rows, $row);
}
$coll = new participantExport([$rows]);
return Excel::download($coll, 'participant-information.xlsx');