MNB PHPExcelDeveloper Guide
v2.0.5

Structured output examples

Structured output Excel examples, explained clearly.

Open a focused example to see the implementation path, copy-ready PHP, expected workbook behavior, and the production boundary that matters.

6examples in this category
21focused categories
5implementation paths
01
Convert a workbook with toStructuredArray()

Return workbook-level source information, every sheet, normalized columns, rows, summaries, warnings, and errors in one PHP array.

EasyDirect API
MnbExcel::read()toStructuredArray()toStructuredWorkbookArray()
convert-a-workbook-with-tostructuredarray.php
<?php

use Mnb\PHPExcel\MnbExcel;

$structured = MnbExcel::read(__DIR__ . '/fixtures/contacts.xlsx')
    ->toStructuredArray([
        'header_row' => true,
        'skip_empty_rows' => true,
        'header_case' => 'snake',
        'rename_headers' => [
            'e_mail' => 'email',
        ],
    ]);

print_r($structured['source']);
print_r($structured['summary']);
print_r($structured['sheets'][0]['rows'] ?? []);
02
Return selected-sheet structured output

Return the structured representation for one selected worksheet without wrapping it in a workbook sheets collection.

EasyDirect API
sheet()toStructuredSheetArray()toStructuredArray()
return-selected-sheet-structured-output.php
<?php

use Mnb\PHPExcel\MnbExcel;

$sheet = MnbExcel::read(__DIR__ . '/fixtures/orders.xlsx')
    ->sheet('Orders')
    ->toStructuredSheetArray([
        'header_row' => true,
        'skip_empty_rows' => true,
        'include_cell_metadata' => false,
    ]);

print_r($sheet['headers']);
print_r($sheet['summary']['row_counts']);

if ($sheet['rows'] === []) {
    echo "No returned data rows.\n";
}

foreach ($sheet['rows'] as $row) {
    print_r($row['values'] ?? $row);
}
03
Create flat structured rows

Flatten each structured record while retaining the original physical source row number for validation and error reporting.

MediumDirect API
toStructuredArray()toStructuredSheetArray()
create-flat-structured-rows.php
<?php

use Mnb\PHPExcel\MnbExcel;

$result = MnbExcel::read(__DIR__ . '/fixtures/import.xlsx')
    ->sheet(1)
    ->toStructuredArray([
        'structure' => 'sheet',
        'header_row' => true,
        'row_format' => 'flat',
        'preserve_original_row_numbers' => true,
        'original_row_number_key' => 'source_row',
    ]);

foreach ($result['rows'] as $row) {
    printf("Source row %d: %s\n", $row['source_row'], $row['email']);
}
04
Return structured JSON from an API

Serialize the full structured workbook directly to compact JSON for a controller or API endpoint without creating a temporary file.

MediumDirect API
toStructuredJson()saveStructuredJson()
return-structured-json-from-an-api.php
<?php

use Mnb\PHPExcel\MnbExcel;

header('Content-Type: application/json; charset=UTF-8');

echo MnbExcel::read(__DIR__ . '/fixtures/students.xlsx')
    ->toStructuredJson([
        'header_row' => true,
        'skip_empty_rows' => true,
        'include_hidden_rows' => false,
        'include_hidden_columns' => false,
        'header_case' => 'snake',
    ], [
        'pretty' => false,
        'trailing_newline' => false,
    ]);
05
Return structured XML from an API

Serialize structured workbook data to XML with a custom root element for integrations that do not consume JSON.

MediumDirect API
toStructuredXml()saveStructuredXml()
return-structured-xml-from-an-api.php
<?php

use Mnb\PHPExcel\MnbExcel;

header('Content-Type: application/xml; charset=UTF-8');

echo MnbExcel::read(__DIR__ . '/fixtures/students.xlsx')
    ->toStructuredXml([
        'header_row' => true,
        'skip_empty_rows' => true,
        'header_case' => 'snake',
    ], [
        'root' => 'structured_workbook',
        'pretty' => true,
    ]);
06
Explain total, empty, skipped, and returned row counts

Read structured sheet counts that distinguish logical row positions, physically stored records, sparse gaps, skipped empty records, the header, and final data rows.

MediumDirect API
ReadSession::toStructuredSheetArray()
explain-total-empty-skipped-and-returned-row-counts.php
<?php


$data = MnbExcel::read(__DIR__ . '/fixtures/orders.xlsx')
    ->sheet('Orders')
    ->toStructuredSheetArray([
        'header_row' => 1,
        'skip_empty_rows' => true,
        'include_cell_metadata' => false,
    ]);

$counts = $data['summary']['row_counts'];

print_r([
    'total' => $counts['total'],
    'source_records' => $counts['source_records'],
    'empty' => $counts['empty'],
    'explicit_empty' => $counts['explicit_empty'],
    'implicit_empty' => $counts['implicit_empty'],
    'skipped_empty' => $counts['skipped_empty'],
    'header' => $counts['header'],
    'returned' => $counts['returned'],
]);