PHP 脚本比较 JSON 和 Excel 数据并输出不一致结果
你可以使用以下的PHP脚本来实现你的需求:
<?php
// 读取JSON文件
$jsonData = file_get_contents('data.json');
$jsonData = json_decode($jsonData, true);
// 读取Excel文件
$excelData = [];
$excelFile = 'data.xlsx';
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load($excelFile);
$worksheet = $spreadsheet->getActiveSheet();
$highestRow = $worksheet->getHighestRow();
for ($row = 2; $row <= $highestRow; $row++) {
$channelName = $worksheet->getCell('A'.$row)->getValue();
$excelData[] = $channelName;
}
// 对比数据并输出到新文件
$newFile = 'result.txt';
$counter = 0;
foreach ($jsonData as $item) {
if (!in_array($item['channelName'], $excelData)) {
file_put_contents($newFile, $item['channelName'].'
', FILE_APPEND);
$counter++;
}
if ($counter >= 100) {
break;
}
}
echo 'Done';
?>
上述脚本首先读取了JSON文件和Excel文件的数据,然后循环比较JSON文件中的channelName数据和Excel文件中的通道名称,并将不一致的数据输出到新文件中。循环最多执行100次,达到100次后停止比较。
请确保你已经安装了phpoffice/phpspreadsheet库,可以使用Composer进行安装。
composer require phpoffice/phpspreadsheet
请将data.json和data.xlsx替换为你实际的JSON文件和Excel文件的路径。输出的不一致数据将保存在result.txt文件中。
原文地址: https://www.cveoy.top/t/topic/pvZ2 著作权归作者所有。请勿转载和采集!