J
J
johjessica72021-08-04 17:49:52
PHP
johjessica7, 2021-08-04 17:49:52

How to put elements into php array?

0}} I loop through it with 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);
}

As a result, I get json like this:

{"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",


{"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":"222222222222222"},{"offer_id" :"A2585","product_id":105761607,"stock":0,"warehouse_id":"22222222222222"},{"offer_id":"A2588","product_id":105763167,"stock":0,"warehouse_id" :"22222222222222"}]}

Doesn't work. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nokimaro, 2021-08-04
@nokimaro

$stocks = [];

foreach ($items as $k => $v) {
    $stocks["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);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question