Re: DBF How to obtain an array containing record number
Posted: Thu Oct 10, 2024 12:42 pm
Hello friends,
In my php4dbf library, I do it exactly the same way.
But I see, despite my efforts to integrate as many functions as possible into the php4dbf library,
I am still far from the source code elegance of Harbour.
Best regards,
Otto
In my php4dbf library, I do it exactly the same way.
But I see, despite my efforts to integrate as many functions as possible into the php4dbf library,
I am still far from the source code elegance of Harbour.
Best regards,
Otto
Code: Select all | Expand
<?php
function parseRecord($record, $fields, $filterFields = []) {
$result = [];
$offset = 1;
foreach ($fields as $field) {
$value = rtrim(substr($record, $offset, $field['length']));
if (empty($filterFields) || in_array($field['name'], $filterFields)) {
if ($field['type'] === 'N') {
$value = is_numeric($value) ? floatval($value) : 0.0;
} elseif ($field['type'] === 'C') {
$value = my_utf8_encode($value);
}
$result[$field['name']] = $value;
}
$offset += $field['length'];
}
return $result;
}
function read_dbf( $file_path) {
$dbData = php4dbf_openFile( $file_path, true, [ 'LAST' ] );
$records = [];
$parsedRecords = [];
for ($i = 1; $i <= php4dbf_eof( $dbData ); $i++) {
$record = php4dbf_readRecordByIndex( $dbData, $i - 1 );
if ($record !== null) {
$parsedRecord = parseRecord( $record, $dbData['fields'], $dbData['filterFields']);
$parsedRecords[] = $parsedRecord;
}
}
php4dbf_closeDbfFile($dbData);
return $parsedRecords;
}
$file_path = "........";
$records = read_dbf($file_path);
?>