PHP Classes

File: examples/05-rows_with_header_values_as_keys.php

Recommend this page to a friend!
  Packages of Sergey Shuchkin   SimpleXLSX   examples/05-rows_with_header_values_as_keys.php   Download  
File: examples/05-rows_with_header_values_as_keys.php
Role: Example script
Content type: text/plain
Description: Example script
Class: SimpleXLSX
Parse and retrieve data from Excel XLS files
Author: By
Last change: 1.0.12
1.0.10
0.9.10
Date: 2 months ago
Size: 1,058 bytes
 

Contents

Class file image Download
<?php /** @noinspection ForgottenDebugOutputInspection */

use Shuchkin\SimpleXLSX;

ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);

require_once
__DIR__.'/../src/SimpleXLSX.php';

echo
'<h1>Rows with header values as keys</h1>';
if (
$xlsx = SimpleXLSX::parse('books.xlsx')) {
   
// Produce array keys from the array values of 1st array element
   
$header_values = $rows = [];

    foreach (
$xlsx->rows() as $k => $r) {
        if (
$k === 0) {
           
$header_values = $r;
            continue;
        }
       
$rows[] = array_combine($header_values, $r);
    }
   
print_r($rows);
/*
Array
(
    [0] => Array
        (
            [ISBN] => 618260307
            [title] => The Hobbit
            [author] => J. R. R. Tolkien
            [publisher] => Houghton Mifflin
            [ctry] => USA
        )

    [1] => Array
        (
            [ISBN] => 908606664
            [title] => Slinky Malinki
            [author] => Lynley Dodd
            [publisher] => Mallinson Rendel
            [ctry] => NZ
        )

)
 */
}