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 :)