How to put this JSON?

I have an array of products and need to match the product name from the array with the item name from this json and get its price.
"data": [
{
 "updated_at": 1576048969000,
 "prices": {
 "last": 5300,
},
 "name": "Jacket",
},
{
 "updated_at": 1576048969000,
 "prices": {
 "last": 2000.12,
},
 "name": "T-shirts",
}
]
April 3rd 20 at 17:34
2 answers
April 3rd 20 at 17:36
Solution
<?php

$jsonProducts = '[
{
 "market_name": "T-shirts",
 "size": "M"
},
{
 "market_name": "T-shirts",
 "size": "L"
 }, 
{
 "market_name": "Jacket",
 "size": "M"
 }, 
{
 "market_name": "Jeans",
 "size": "XL"
}
]';

$jsonPrice = '{
 "data": [
{
 "updated_at": 1576048969000,
 "prices": {
 "last": 5300
},
 "name": "Jacket"
},
{
 "updated_at": 1576048969000,
 "prices": {
 "last": 2000.12
},
 "name": "T-shirts"
}
]
}';

$products = json_decode($jsonProducts, true);
$price = array_column(json_decode($jsonPrice, true)['data'], 'prices', 'name');

$result = array_map(function($item) use ($price) {
 $product = new \stdClass();
 $product->market_name = $item['market_name'];
 $product->size = $item['size'];
 $product->price = $price[$item['market_name']]['last'] ?? null;

 return $product;
}, $products);

$result = json_encode($result, JSON_PRETTY_PRINT);

var_dump($result);
// [
// {
// "market_name": "T-shirts",
// "size": "M",
// "price": 2000.12
// },
// {
// "market_name": "T-shirts",
// "size": "L",
// "price": 2000.12
// },
// {
// "market_name": "Jacket",
// "size": "M",
// "price": 5300
// },
// {
// "market_name": "Jeans",
// "size": "XL",
// "price": null
// }
// ]


sandbox.onlinephpfunctions.com/code/935656788a07af...
I have in stock have this structure:
$products = '[{
 "market_name": "T-shirts",
 "size": "L"
}, {
 "market_name": "Jacket",
 "size": "M"
}]';
//Price lists
$decodedData = <<<JSON
{
 "data": [
{
 "updated_at": 1576048969000,
 "prices": {
 "last": 5300
},
 "name": "Jacket"
},
{
 "updated_at": 1576048969000,
 "prices": {
 "last": 2000.12
},
 "name": "T-shirts"
}
]
}
JSON
;

I need to make a product like this:

{
 "market_name": "T-shirts",
 "size": "L",
"price":2000.12
}
- enrico.Braun commented on April 3rd 20 at 17:39
@dan_Runolfsson55has worked for you and updated the answer :) - nelda.Gusikowski69 commented on April 3rd 20 at 17:42
@Hardy, sandbox.onlinephpfunctions.com/code/c9bbf2e2c462b3... have market_name null when you var_dump out there - enrico.Braun commented on April 3rd 20 at 17:45
@dan_Runolfsson55, to name or replace the use of the PD array data :)
I understand... inside anonymous can thadaniti the data and understand everything - nelda.Gusikowski69 commented on April 3rd 20 at 17:48
@HardyYes I already understood, just is not an option, because it eliminates all the data which is missing price, or if there are duplicates in the array items) - enrico.Braun commented on April 3rd 20 at 17:51
@dan_Runolfsson55, man, well, it's hard to play constantly changing rules :):):):)

are understood? to reduce everything to a convenient format and then to combine functions like array_map/array_reduce - nelda.Gusikowski69 commented on April 3rd 20 at 17:54
@dan_Runolfsson55, at the expense of duplicates — if two products with different price in the same array and the same product with the XL size, what price to take? - nelda.Gusikowski69 commented on April 3rd 20 at 17:57
@dan_Runolfsson55,
sandbox.onlinephpfunctions.com/code/1bd55e35dab717... - nelda.Gusikowski69 commented on April 3rd 20 at 18:00
@Hardy, and there is generally not important, just need the price of goods depended from the name. Sorry for the constant additions, just 2 days sitting on this parser is very weak in them. - enrico.Braun commented on April 3rd 20 at 18:03
@Hardy, ie in price.json has only the product name and price, I have products.json has the name of the product and available size, I just need to pull the price from the price list and do this:
{
 "market_name": "T-shirts",
 "size": "L",
"price":5000
 }

But the product may be a duplicate of this code and just sift through it and left only 1 item - enrico.Braun commented on April 3rd 20 at 18:06
@dan_Runolfsson55, well with the doubles, Yes — what to do? Two products with the same name and different price ? :) - nelda.Gusikowski69 commented on April 3rd 20 at 18:09
@Hardy, Just products.json if you have 2 of the same item, just assign them a price which is price.json and:
Like this:
products.json
{
 "market_name": "T-shirts",
 "size": "L",
},
{
 "market_name": "T-shirts",
 "size": "L",
 }

price.json
{
 "name": "T-shirts",
"price":5000
 }

In this case, it will just be the same 2 product, but will be added "price":5000 - enrico.Braun commented on April 3rd 20 at 18:12
@dan_Runolfsson55, :):):) well that was the last time you help on this task...

in the source array with dimensions added with the re-name and added a product which is not in the array with the prices... look at the final bottom ovata - nelda.Gusikowski69 commented on April 3rd 20 at 18:15
April 3rd 20 at 17:38
You curly brackets at the beginning and end accidentally removed or a broken JSON?
If the first option, nothing complicated, just out:
$data = json_decode($json, true);

foreach ($data['data'] as $item) {
print_r($item);
}
And so did not need a nested loop if I have only 2 of the array need to sort through? Roughly speaking, I take your array of goods available and this json with the list of prices of all goods and glue it into 1 array to get the price of my goods which I have at the moment. - enrico.Braun commented on April 3rd 20 at 17:41
All products that you have - create the array and check if bypass JSON'it for presence in this array. You can also key the name of the goods to make for convenience.
$products = ['Jacket' => 0];

$data = json_decode($json, true);

foreach ($data['data'] as $item) {
 if (!empty($products[$item['name']])) $products[$item['name']] = $item['prices']['last'];
}
- monserrat.Langosh commented on April 3rd 20 at 17:44

Find more questions by tags PHP