创建一个仅与昨天不同的 JSON

Creating a JSON that only has the differences from yesterday

我正在为这件事绞尽脑汁,但我无法找到可能的解决方案,甚至不知道如何做到这一点。

我有一个脚本(如下),可以将多个 JSON 文件组合并重写为一个 JSON 文件。我有一个 WooCommerce 网上商店,它通过 WP All 导入 JSON 文件到网上商店。一夜之间 WP All Import 使用 JSON 文件中的更改更新所有产品。问题是 WP All Import 需要检查 JSON 中的所有项目,这需要一段时间。

我的问题: 是否可以通过 PHP 将每个项目的 'last-updated' 日期添加到 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);

问候 伊利

比较旧的和新的,并通过 cron 每天创建一个仅包含更改的精简文件。

Utility.php

    <?php
error_reporting(E_ALL);

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');
}

function getFileName($type, $language = 'en') {

    return sprintf('dsales_watchjewel_combined_%s_%s.json', $type, $language);
}

$urls = [
    'URL',
    'URL'
];

$compare = [
    'ParentSKU',
    'Category',
    'Title',
    'Description',
    'Brand',
    'RRP',
    'Price',
    'Stock',
    'SPEC_Large size',
    'SPEC_COLOR',
    'SPEC_SIZE  W x D x H (mm)',
    'SPEC_TYPE ',
    'SPEC_BACKLIGHT',
    'SPEC_SNOOZE FUNCTION',
    'SPEC_RADIO CONTROLLED',
    'SPEC_BATTERY  TYPE',
    'SPEC_WEIGHT  gram',
    'SPEC_OTHERS',
    'SPEC_Model_Name',
    'SPEC_Model Name',
    'SPEC_Family',
    'SPEC_Weight-Gram',
    'SPEC_Type',
    'SPEC_Target_Audience',
    'SPEC_Target_audience',
    'SPEC_Metal',
    'SPEC_Display_Type',
    'SPEC_Jewel_type',
    'SPEC_Color',
    'SPEC_Movement',
    'SPEC_Metal_type',
    'SPEC_Features_1',
    'SPEC_Finish',
    'SPEC_Metal Color',
    'SPEC_Width',
    'SPEC_Features_2',
    'SPEC_Material_type',
    'SPEC_Material_color',
    'SPEC_Features_3',
    'SPEC_Weight',
    'SPEC_Features_4',
    'SPEC_Size',
    'SPEC_Clasp_type',
    'SPEC_Stone',
    'SPEC_Back_finding',
    'SPEC_Features_5',
    'SPEC_Calendar',
    'SPEC_Ring_size',
    'SPEC_Setting',
    'SPEC_Ring_resizable',
    'SPEC_ATM',
    'SPEC_Bullet point 1',
    'SPEC_Bullet point 2',
    'SPEC_Height-cm',
    'SPEC_Case_material',
    'SPEC_Case_Style',
    'SPEC_Length-cm',
    'SPEC_Bullet point 3',
    'SPEC_Bullet point 4',
    'SPEC_Width-cm',
    'SPEC_Case_color',
    'SPEC_Dial_Color',
    'SPEC_Diameter-cm',
    'SPEC_Bullet point 5',
    'SPEC_Gem_shape',
    'SPEC_Band_Material',
    'SPEC_Band_Color',
    'SPEC_Gem_type',
    'SPEC_Diamond_quantity',
    'SPEC_Band_Width_mm',
    'SPEC_Band_Length_cm',
    'SPEC_Total_Diamond_Weight',
    'SPEC_Diamond_color',
    'SPEC_Case_Diameter_mm',
    'SPEC_Case_Width_mm',
    'SPEC_Diamond_clarity',
    'SPEC_Diamond_setting',
    'SPEC_Case_Length_mm',
    'SPEC_Case_Thickness_mm',
    'SPEC_Diamond_shape',
    'SPEC_Gem_Type 2',
    'SPEC_Bezel_Function',
    'SPEC_Bezel_Material_Type',
    'SPEC_Total_Gem_Weight',
    'SPEC_Gem_color',
    'SPEC_Gem_Type',
    'SPEC_Gem_quantity',
    'SPEC_Diamond_Weight_ct',
    'SPEC_Stamp_Type',
    'SPEC_Pearl_type',
    'SPEC_Pearl_color',
    'SPEC_Clasp_Type',
    'SPEC_Weight_g',
    'SPEC_Pearl_size',
    'SPEC_Pearl_shape',
    'SPEC_Dial_Window_Material',
    'SPEC_Extra info',
    'SPEC_Chain Type',
    'SPEC_Battery',
    'SPEC_Necklace_type',
    'SPEC_Charm_shape',
    'SPEC_Box_Length_cm',
    'SPEC_Box Height cm',
    'SPEC_Charm_type',
    'SPEC_Ring type',
    'SPEC_Box Width cm',
    'SPEC_Bracelet_type',
    'SPEC_Product Description',
    'SPEC_Brand Description',
    'Images'
];

Run.php

    <?php
include 'utility.php';

$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(getFileName('new'), 'w');
fwrite($fp, $new);
fclose($fp);

Compare.php

    <?php
include 'utility.php';

if (!file_exists(getFileName('old'))) {
    file_put_contents(getFileName('old'), json_encode([]));
}

$oldData = json_decode(file_get_contents(getFileName('old')), true);
$newData = json_decode(file_get_contents(getFileName('new')), true);

$refinedData = [];

echo '<pre>';

if (empty($oldData)) {
    file_put_contents(getFileName('old'), json_encode($newData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

    exit('Script needs to be run first to sync');
}
foreach ($newData as $newProduct) {

    $isNotTheSame = false;
    $found = false;

    foreach ($oldData as $oldProduct) {

        if ($newProduct['SKU'] == $oldProduct['SKU']) {

            foreach ($compare as $key) {

                if (
                    (isset($newProduct[$key]) && isset($oldProduct[$key])) &&
                    ($newProduct[$key] != $oldProduct[$key])
                ) {
                    $isNotTheSame = $key;
                }
            }

            $found = true;

            if ($isNotTheSame) {
                $refinedData[] = $newProduct;
            }

        }

    }

    /**
     * If the product is not found in the old list
     * add it to the refined list to be created
     */
    if (!$found) {
        $refinedData[] = $newProduct;
    }

}

file_put_contents(getFileName('old'), json_encode($newData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

echo json_encode($refinedData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); // Echo the result

file_put_contents(getFileName('refined'), json_encode($refinedData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

$date = date('dmy-His');
file_put_contents(getFileName('refined_' . $date ), json_encode($refinedData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));