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.
01Convert a workbook with toStructuredArray()Return workbook-level source information, every sheet, normalized columns, rows, summaries, warnings, and errors in one PHP array.
EasyDirect API+
Return workbook-level source information, every sheet, normalized columns, rows, summaries, warnings, and errors in one PHP array.
MnbExcel::read()toStructuredArray()toStructuredWorkbookArray()<?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'] ?? []);02Return selected-sheet structured outputReturn the structured representation for one selected worksheet without wrapping it in a workbook sheets collection.
EasyDirect API+
Return the structured representation for one selected worksheet without wrapping it in a workbook sheets collection.
sheet()toStructuredSheetArray()toStructuredArray()<?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);
}03Create flat structured rowsFlatten each structured record while retaining the original physical source row number for validation and error reporting.
MediumDirect API+
Flatten each structured record while retaining the original physical source row number for validation and error reporting.
toStructuredArray()toStructuredSheetArray()<?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']);
}04Return structured JSON from an APISerialize the full structured workbook directly to compact JSON for a controller or API endpoint without creating a temporary file.
MediumDirect API+
Serialize the full structured workbook directly to compact JSON for a controller or API endpoint without creating a temporary file.
toStructuredJson()saveStructuredJson()<?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,
]);05Return structured XML from an APISerialize structured workbook data to XML with a custom root element for integrations that do not consume JSON.
MediumDirect API+
Serialize structured workbook data to XML with a custom root element for integrations that do not consume JSON.
toStructuredXml()saveStructuredXml()<?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,
]);06Explain total, empty, skipped, and returned row countsRead structured sheet counts that distinguish logical row positions, physically stored records, sparse gaps, skipped empty records, the header, and final data rows.
MediumDirect API+
Read structured sheet counts that distinguish logical row positions, physically stored records, sparse gaps, skipped empty records, the header, and final data rows.
ReadSession::toStructuredSheetArray()<?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'],
]);