<?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...