Quitar la Hoja de los Padres de la Matriz de Importación para Colección o Array en Laravel Excel del Paquete

0

Pregunta

Quiero importar de Excel, pero sólo en la hoja de índice[0], pero cuando uso toCollection o método toArray las filas envuelto en hoja de índice de la matriz:

Quiero quitar la envuelta de los padres de la matriz, ¿cómo hacerlo?

Aquí está mi código:

Import.php

<?php

namespace App\Imports;

use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class Import implements SkipsEmptyRows,
                        WithCalculatedFormulas,
                        WithMultipleSheets
{
    use Importable;

    /**
     * Select sheet by index and how they are
     * mapped to specific import object.
     *
     * @return array
     */
    public function sheets(): array
    {
        return [
            0 => new static(),
        ];
    }
}

Controlador:

$filePath = 'import-excel.xlsx';
$import = new Import();
$rows = $import->toCollection($filePath);

dd($rows->toArray());

Resultado Esperado:

[0] => Array(
    [0] => *Contact Name
    [1] => *Contact Email
    [2] => *Contact Address
)

[1] => Array (
    [0] => Talia Oktaviani S.Psi
    [1] => [email protected]
    [2] => Ds. Abdul No. 618 Tarakan 11408 KalU
)

Resultado Real:

Array(  <== ***I want remove this.***
    [0] => Array(  <== ***I want remove this.***
        [0] => Array(
            [0] => *Contact Name
            [1] => *Contact Email
            [2] => *Contact Address
        )

        [1] => Array (
            [0] => Talia Oktaviani S.Psi
            [1] => [email protected]
            [2] => Ds. Abdul No. 618 Tarakan 11408 KalU
        )
    )  <== ***I want remove this.***
)  <== ***I want remove this.***
arrays excel import laravel
2021-11-24 00:35:38
1

Mejor respuesta

1

Puede eliminar si el uso de colapso de la función de

$filePath = 'import-excel.xlsx';
$import = new Import();
$rows = $import->toCollection($filePath)->collapse();

dd($rows->toArray());
2021-11-25 14:29:13

En otros idiomas

Esta página está en otros idiomas

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Slovenský
..................................................................................................................