我对此感到头疼,我无法找到可能的解决方案,甚至无法找到如何做到这一点的想法。
我有一个脚本(如下),它将多个 JSON 文件组合并重写为一个 JSON 文件。我有一个通过 WP All 导入的 WooCommerce 网店将 JSON 文件导入网店。隔夜 WP All Import 使用 JSON 文件中的更改更新所有产品。问题是 WP All Import 需要检查 JSON 中的所有项目,这需要一段时间。
我的问题:当它发现该项目的更改时,是否可以通过 PHP 将每个项目的“最后更新”日期添加到 JSON 中?是否可以只生成一个包含找到的更改的 JSON 文件?也许它可以将一个新的 JSON 文件与昨天的 JSON 文件进行比较,并生成一个包含更改的文件?
当前代码:
<?php
error_reporting(E_ALL);
// API URLS
$urls = array
( 'URL'
, 'URL'
);
// Remove FALSE but not 0 and null
function RemoveFalseButNotZero($value) {
return ($value || is_numeric($value) || $value === null );
}
// Function to delete all empty JSON items
function array_filter_recursive($input)
{
foreach ($input as &$value)
{
if (is_array($value))
{
$value = array_filter_recursive($value);
}
}
return array_filter($input, 'RemoveFalseButNotZero');
}
$sets = array();
foreach ($urls as $url)
{
$json = file_get_contents($url); // Collect all URLS
$data = json_decode($json, true); // Decode JSON to read and edit
foreach ($data as $key => $innerObject) { // Delete each item that is not needed
if (isset(
$innerObject['SPEC_Bullet point 1'],
$innerObject['SPEC_Bullet point 2'],
$innerObject['SPEC_Bullet point 3'],
$innerObject['SPEC_Bullet point 4'],
$innerObject['SPEC_Bullet point 5'],
$innerObject['SPEC_Extra info'],
$innerObject['SPEC_Keywords'],
$innerObject['SPEC_Featured'],
$innerObject['SPEC_Bullet point 1 TEXT'],
$innerObject['SPEC_Bullet point 2 LIST'],
$innerObject['SPEC_Bullet Point 1'],
$innerObject['SPEC_Bullet point 2 TEXT'],
$innerObject['SPEC_Bullet Point 1 TEXT'],
$innerObject['SPEC_Bullet Point 2'],
$innerObject['SPEC_Bullet point 3 LIST'],
$innerObject['SPEC_Bullet Point 2 TEXT'],
$innerObject['SPEC_Bullet point 3 TEXT'],
$innerObject['SPEC_Bullet point 4 LIST'],
$innerObject['SPEC_Bullet point 4 TEXT'],
$innerObject['SPEC_Bullet Point 3'],
$innerObject['SPEC_Bullet Point 3 TEXT'],
$innerObject['SPEC_Bullet point 5 LIST'],
$innerObject['SPEC_Bullet point 5 TEXT'],
$innerObject['SPEC_Bullet Point 4 TEXT'],
$innerObject['SPEC_Bullet Point 5'],
$innerObject['SPEC_Bullet Point 5 TEXT'],
$innerObject['SPEC_Bullet point 1 LIST']
)) {
unset(
$data[$key]['SPEC_Bullet point 1'],
$data[$key]['SPEC_Bullet point 2'],
$data[$key]['SPEC_Bullet point 3'],
$data[$key]['SPEC_Bullet point 4'],
$data[$key]['SPEC_Bullet point 5'],
$data[$key]['SPEC_Extra info'],
$data[$key]['SPEC_Keywords'],
$data[$key]['SPEC_Featured'],
$data[$key]['SPEC_Bullet point 1 TEXT'],
$data[$key]['SPEC_Bullet point 2 LIST'],
$data[$key]['SPEC_Bullet Point 1'],
$data[$key]['SPEC_Bullet point 2 TEXT'],
$data[$key]['SPEC_Bullet Point 1 TEXT'],
$data[$key]['SPEC_Bullet Point 2'],
$data[$key]['SPEC_Bullet point 3 LIST'],
$data[$key]['SPEC_Bullet Point 2 TEXT'],
$data[$key]['SPEC_Bullet point 3 TEXT'],
$data[$key]['SPEC_Bullet point 4 LIST'],
$data[$key]['SPEC_Bullet point 4 TEXT'],
$data[$key]['SPEC_Bullet Point 3'],
$data[$key]['SPEC_Bullet Point 3 TEXT'],
$data[$key]['SPEC_Bullet point 5 LIST'],
$data[$key]['SPEC_Bullet point 5 TEXT'],
$data[$key]['SPEC_Bullet Point 4 TEXT'],
$data[$key]['SPEC_Bullet Point 5'],
$data[$key]['SPEC_Bullet Point 5 TEXT'],
$data[$key]['SPEC_Bullet point 1 LIST']
);
}
}
$json = substr($json, 1, -1); // Delete upper level brackets
$sets = array_merge($sets, $data); // Merge all URLS in one single JSON file
$filteredData = array_filter_recursive($sets); // Filter the empty items and delete them
}
$new = json_encode($filteredData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); // Encode back to JSON pretty print and remove unescaped slashes
echo '<pre>'; // Add breaks
echo $new; // Echo the result
// Write the new file
$fp = fopen('dsales_watchjewel_combined_en.json', 'w');
fwrite($fp, $new);
fclose($fp);
问候伊利