Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert to array with keys from first row by PHPExcel?

Sorry, cant find what i need. I have xls/xlsx. Then i get smth like this:

array
  0 => 
    array
    0 => string 'NameFirstColumn'
    1 => string 'NameSecondColumn'
  1 => 
    array
    0 => string 'qqq'
    1 => float 30
  2 => 
    array
    0 => string 'www'
    1 => float 20

First row is a header with names of values. How to make PHPExcel convert to array looks like:

array
  0 => 
    array
    NameFirstColumn => string 'qqq'
    NameSecondColumn => float 30
  1 => 
    array
    NameFirstColumn => string 'www'
    NameSecondColumn => float 20
like image 412
Philipp Klemeshov Avatar asked Feb 28 '16 17:02

Philipp Klemeshov


1 Answers

Assuming that you already have this array in $array

$headings = array_shift($array);
array_walk(
    $array,
    function (&$row) use ($headings) {
        $row = array_combine($headings, $row);
    }
);
like image 196
Mark Baker Avatar answered Nov 14 '22 14:11

Mark Baker