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
21supported categories
5implementation paths
Monolithic library
Structured output examples with copy-ready, production-minded PHP.
Each example identifies the exact methods it uses and can be opened independently from the right-side index.
- 6
- examples
- 10
- documented methods
MnbExcel::read()toStructuredArray()toStructuredWorkbookArray()<?php
declare(strict_types=1);
/*
Summary:
Return workbook-level source information, every sheet, normalized columns, rows, summaries,
warnings, and errors in one PHP array.
Implementation note:
toStructuredArray() returns workbook scope by default. It is designed for APIs, import previews,
diagnostics, and audit-friendly processing rather than simple row-only imports.
*/
use Mnb\PHPExcel\MnbExcel;
$workbookPath = __DIR__ . '/fixtures/contacts.xlsx';
if (!is_file($workbookPath)) {
throw new RuntimeException(sprintf('Workbook not found: %s', $workbookPath));
}
$structured = MnbExcel::read($workbookPath)
->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'] ?? []);sheet()toStructuredSheetArray()toStructuredArray()<?php
declare(strict_types=1);
/*
Summary:
Return the structured representation for one selected worksheet without wrapping it in a
workbook sheets collection.
Implementation note:
The equivalent generic call is toStructuredArray(["structure" => "sheet"]). The result is a
native PHP array, not a ReadSession; use array keys for rows, summaries, warnings, and errors.
*/
use Mnb\PHPExcel\MnbExcel;
$workbookPath = __DIR__ . '/fixtures/orders.xlsx';
if (!is_file($workbookPath)) {
throw new RuntimeException(sprintf('Workbook not found: %s', $workbookPath));
}
$sheet = MnbExcel::read($workbookPath)
->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);
}toStructuredArray()toStructuredSheetArray()<?php
declare(strict_types=1);
/*
Summary:
Flatten each structured record while retaining the original physical source row number for
validation and error reporting.
Implementation note:
Flat rows are convenient for validators and SQL mapping. The default nested format keeps row
metadata separate from the values payload.
*/
use Mnb\PHPExcel\MnbExcel;
$workbookPath = __DIR__ . '/fixtures/import.xlsx';
if (!is_file($workbookPath)) {
throw new RuntimeException(sprintf('Workbook not found: %s', $workbookPath));
}
$result = MnbExcel::read($workbookPath)
->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']);
}toStructuredJson()saveStructuredJson()<?php
declare(strict_types=1);
/*
Summary:
Serialize the full structured workbook directly to compact JSON for a controller or API endpoint
without creating a temporary file.
Implementation note:
Use saveStructuredJson() only when the JSON must be written to disk. toStructuredJson() returns
a string suitable for framework responses.
*/
use Mnb\PHPExcel\MnbExcel;
$workbookPath = __DIR__ . '/fixtures/students.xlsx';
if (!is_file($workbookPath)) {
throw new RuntimeException(sprintf('Workbook not found: %s', $workbookPath));
}
header('Content-Type: application/json; charset=UTF-8');
echo MnbExcel::read($workbookPath)
->toStructuredJson([
'header_row' => true,
'skip_empty_rows' => true,
'include_hidden_rows' => false,
'include_hidden_columns' => false,
'header_case' => 'snake',
], [
'pretty' => false,
'trailing_newline' => false,
]);toStructuredXml()saveStructuredXml()<?php
declare(strict_types=1);
/*
Summary:
Serialize structured workbook data to XML with a custom root element for integrations that do
not consume JSON.
Implementation note:
saveStructuredXml() provides the same structured payload through the atomic file-writing path.
*/
use Mnb\PHPExcel\MnbExcel;
$workbookPath = __DIR__ . '/fixtures/students.xlsx';
if (!is_file($workbookPath)) {
throw new RuntimeException(sprintf('Workbook not found: %s', $workbookPath));
}
header('Content-Type: application/xml; charset=UTF-8');
echo MnbExcel::read($workbookPath)
->toStructuredXml([
'header_row' => true,
'skip_empty_rows' => true,
'header_case' => 'snake',
], [
'root' => 'structured_workbook',
'pretty' => true,
]);ReadSession::toStructuredSheetArray()<?php
declare(strict_types=1);
/*
Summary:
Read structured sheet counts that distinguish logical row positions, physically stored records,
sparse gaps, skipped empty records, the header, and final data rows.
Implementation note:
total includes sparse physical row-number gaps. returned is the final number of data records
after header and empty-row processing.
*/
$workbookPath = __DIR__ . '/fixtures/orders.xlsx';
if (!is_file($workbookPath)) {
throw new RuntimeException(sprintf('Workbook not found: %s', $workbookPath));
}
<?php
$data = MnbExcel::read($workbookPath)
->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'],
]);No matching example found.Try a method name, task, or result.
Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue