0

Am quite new to PHP programing and am trying to achieve the below requirement but no luck so far, any help will be appreciated 😊 .

I have JSON file which contains product information as follows:

 [{
    "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"
}]

Am reading the file using the following code:

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

As you can see in the first three product the Description are the same except the last string the variation of that product like 50 ml , 100 ml , 200 ml.

I would like to write code to compare the products and delete the variation of the product and keep only the string according to two conditions.

  • Description string should be identical except the variation
  • The brand id should be the same for the products description that match

If both condition above not meet then nothing should be changed to that products.

The output should be as follow and should be saved to another file:

[{
    "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"
}]

Thanks in advance :)

3
  • yes similar part are always in the beginning Commented Jan 20, 2018 at 9:49
  • You should attempt to solve this problem yourself first and share your attempt. This is a very broad question as it stands. Commented Jan 20, 2018 at 9:51
  • am trying to figure it out but am running out of time that why i post it here hopping to get at least a hint about how to do it :) Commented Jan 20, 2018 at 9:54

1 Answer 1

1

Here's a solution:

The idea is to decode as array and for each element check if there's other descriptions with a common root. This is done via a regex which matches (string part) (number 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);

Here's a run: https://eval.in/938746

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking ā€œPost Your Answerā€, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.