Php比较Json键值并在满足某些条件时更改值(String)

时间:2018-01-20 09:43:01

标签: php json

我对PHP编程很新,我正在努力达到以下要求,但到目前为止没有运气,任何帮助将不胜感激。

我有JSON文件,其中包含以下产品信息:

 [{
    "Id": 3322891,
    "EANs": ["12131331313", "34554542401860"],
    "Description": "test one perf 200 ml",
    "SetContent": null,
    "Price": 1.16,
    "PVR": 8.10,
    "Stock": 1000,
    "BrandId": "10000",
    "BrandName": "test",
    "Gender": "Woman",
    "Families": ["Perfume"],
    "Kgs": 0.4420121,
    "Ancho": 90121,
    "Alto": 168121,
    "Fondo": 4125,
    "IVA": 21121.00,
    "Fecha": "2021-11-24",
    "Contenido": "200 ml",
    "Gama": "3980000"
},
 {
    "Id": 33891212,
    "EANs": ["360734122122402065"],
    "Description": "test one perf 100 ml",
    "SetContent": null,
    "Price": 220.93,
    "PVR": 620.45,
    "Stock": 30,
    "BrandId": "10000",
    "BrandName": "test",
    "Gender": "Woman",
    "Families": ["Perfume"],
    "Kgs": 0.25002,
    "Ancho": 751,
    "Alto": 1411,
    "Fondo": 317,
    "IVA": 211.00,
    "Fecha": "2021-08-24",
    "Contenido": "100 ml",
    "Gama": "39800"
},{
    "Id": 33893122,
    "EANs": ["3607121212342401945"],
    "Description": "test one perf 50 ml",
    "SetContent": null,
    "Price": 111.92,
    "PVR": 400.30,
    "Stock": 220,
    "BrandId": "10000",
    "BrandName": "test",
    "Gender": "Woman",
    "Families": ["Perfume"],
    "Kgs": 0.14020,
    "Ancho": 623,
    "Alto": 1128,
    "Fondo": 321,
    "IVA": 221.00,
    "Fecha": "2021-12-09",
    "Contenido": "50 ml",
    "Gama": "39280"
},{
    "Id": 33893122,
    "EANs": ["3607121212342401945"],
    "Description": "test sec perf 50 ml",
    "SetContent": null,
    "Price": 111.92,
    "PVR": 400.30,
    "Stock": 220,
    "BrandId": "20000",
    "BrandName": "test",
    "Gender": "Woman",
    "Families": ["Perfume"],
    "Kgs": 0.14020,
    "Ancho": 623,
    "Alto": 1128,
    "Fondo": 321,
    "IVA": 221.00,
    "Fecha": "2021-12-09",
    "Contenido": "50 ml",
    "Gama": "39280"
}]

使用以下代码阅读文件:

<?php
//test to read from json file don't touch it
$url = 'similar-product.json'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
 $characters = json_decode($data); // decode the JSON feed

//echo $characters[0]->Description;

 foreach ($characters as $character) {
echo $character->Description . '<br>';
 }

正如你在前三个产品中看到的那样,描述是相同的,除了最后一个字符串,该产品的变化如50毫升,100毫升,200毫升。

我想编写代码来比较产品并删除产品的变体,并根据两个条件只保留字符串。

  • 除变体
  • 外,描述字符串应相同
  • 与匹配
  • 的商品说明的品牌ID应相同

如果上述两个条件都不符合,则不应将任何内容更改为该产品。

输出应如下所示,并应保存到另一个文件中:

[{
    "Id": 3322891,
    "EANs": ["12131331313", "34554542401860"],
    "Description": "test one perf",
    "SetContent": null,
    "Price": 1.16,
    "PVR": 8.10,
    "Stock": 1000,
    "BrandId": "10000",
    "BrandName": "test",
    "Gender": "Woman",
    "Families": ["Perfume"],
    "Kgs": 0.4420121,
    "Ancho": 90121,
    "Alto": 168121,
    "Fondo": 4125,
    "IVA": 21121.00,
    "Fecha": "2021-11-24",
    "Contenido": "200 ml",
    "Gama": "3980000"
},
 {
    "Id": 33891212,
    "EANs": ["360734122122402065"],
    "Description": "test one perf",
    "SetContent": null,
    "Price": 220.93,
    "PVR": 620.45,
    "Stock": 30,
    "BrandId": "10000",
    "BrandName": "test",
    "Gender": "Woman",
    "Families": ["Perfume"],
    "Kgs": 0.25002,
    "Ancho": 751,
    "Alto": 1411,
    "Fondo": 317,
    "IVA": 211.00,
    "Fecha": "2021-08-24",
    "Contenido": "100 ml",
    "Gama": "39800"
},{
    "Id": 33893122,
    "EANs": ["3607121212342401945"],
    "Description": "test one perf",
    "SetContent": null,
    "Price": 111.92,
    "PVR": 400.30,
    "Stock": 220,
    "BrandId": "10000",
    "BrandName": "test",
    "Gender": "Woman",
    "Families": ["Perfume"],
    "Kgs": 0.14020,
    "Ancho": 623,
    "Alto": 1128,
    "Fondo": 321,
    "IVA": 221.00,
    "Fecha": "2021-12-09",
    "Contenido": "50 ml",
    "Gama": "39280"
},{
    "Id": 33893122,
    "EANs": ["3607121212342401945"],
    "Description": "test sec perf 50 ml",
    "SetContent": null,
    "Price": 111.92,
    "PVR": 400.30,
    "Stock": 220,
    "BrandId": "20000",
    "BrandName": "test",
    "Gender": "Woman",
    "Families": ["Perfume"],
    "Kgs": 0.14020,
    "Ancho": 623,
    "Alto": 1128,
    "Fondo": 321,
    "IVA": 221.00,
    "Fecha": "2021-12-09",
    "Contenido": "50 ml",
    "Gama": "39280"
}]

提前致谢:)

1 个答案:

答案 0 :(得分:1)

这是一个解决方案:

想法是解码为数组,并为每个元素检查是否存在具有公共根的其他描述。这是通过匹配(字符串部分)(数字ml)的正则表达式完成的。

$url = 'similar-product.json'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable

$characters = json_decode($data, true); // I prefer arrays
function findCommonRoot($allDescriptions, $index) {
     preg_match("/(.*) (\d+ ml)/", $allDescriptions[$index], $matches);

     if (isset($matches[1])) {
         foreach ($allDescriptions as $i => $description) {
               if ($i != $index && strncmp($matches[1], $description, strlen($matches[1])) === 0) {
                      return $matches[1];

               }
         }
     }
     return $allDescriptions[$index];
}
$allDescriptions = array_column($characters, "Description");
$result = [];
foreach ($characters as $index => $character) {
      $result[$index] = $character;
      $result[$index]["Description"] = findCommonRoot($allDescriptions, $index); 
}

 print_r($result);

这是一个运行:https://eval.in/938746