Есть массив $items, его содержимое:
[{"product_id":105756483,"offer_id":"A2274","stock":{"coming":0,"present":0,"reserved":0}},{"product_id":105757609,"offer_id":"A2275","stock":{"coming":0,"present":0,"reserved":0}},{"product_id":105759812,"offer_id":"A1143","stock":{"coming":0,"present":0,"reserved":0}},{"product_id":105761607,"offer_id":"A2585","stock":{"coming":0,"present":0,"reserved":0}},{"product_id":105763167,"offer_id":"A2588","stock":{"coming":0,"present":0,"reserved":0}}
Перебираю его с помощью foreach:
$stocks = [];
foreach ($items as $k => $v) {
$stocks = [
'offer_id' => $v['offer_id'],
'product_id' => $v['product_id'],
'stock' => $v['stock']['present'],
'warehouse_id' => '22222222222222',
];
$array = json_encode($stocks);
print_r($array);
}
В результате получаю json такого вида:
{"offer_id":"A2274","product_id":105756483,"stock":0,"warehouse_id":"22222222222222"}{"offer_id":"A2275","product_id":105757609,"stock":0,"warehouse_id":"22222222222222"}{"offer_id":"A1143","product_id":105759812,"stock":0,"warehouse_id":"22222222222222"}{"offer_id":"A2585","product_id":105761607,"stock":0,"warehouse_id":"22222222222222"}{"offer_id":"A2588","product_id":105763167,"stock":0,"warehouse_id":"22222222222222"}
А нужно получить json вот в таком виде:
{"stocks":[{"offer_id":"A2274","product_id":105756483,"stock":0,"warehouse_id":"22222222222222"},{"offer_id":"A2275","product_id":105757609,"stock":0,"warehouse_id":"22222222222222"},{"offer_id":"A1143","product_id":105759812,"stock":0,"warehouse_id":"22222222222222"},{"offer_id":"A2585","product_id":105761607,"stock":0,"warehouse_id":"22222222222222"},{"offer_id":"A2588","product_id":105763167,"stock":0,"warehouse_id":"22222222222222"}]}
Не получается. Что делаю не так?
-
Вопрос задан
-
77 просмотров