i need to create a json from an array of values like this:
[items_list] => Array
(
[0] => FattureInCloudModelReceivedDocumentItemsListItem Object
(
[openAPINullablesSetToNull:protected] => Array
(
[0] => product_id
)
[container:protected] => Array
(
[id] => 317193954
[product_id] =>
[code] =>
[name] => Gestione pratica
[measure] =>
[net_price] => 0.6
[category] =>
[qty] => 1
[vat] => FattureModelVatType Object
(
[container:protected] => Array
(
[id] => 2531827
[value] => 22
[description] => 22%-GENERICO
[notes] =>
[e_invoice] =>
[ei_type] =>
[ei_description] =>
[editable] =>
[is_disabled] =>
)
)
[stock] => 0
)
)
)
i need to get a json that looks like this:
"lines": [
{
"desc": "Some additional text",
"qty": "10",
"subprice": 100,
"tva_tx": "20.000",
"fk_product": "1",
"remise_percent": "0",
"date_start": 1494252128,
"date_end": 1494194400,
"info_bits": "0"
}
],
I tried to use a foreach loop but to no avail, it just returns the values from the last data set
this is my cycle:
foreach($result['items_list'] as $item) {
$myObj->lines = new stdClass;
$myObj->lines->desc = $item['name'];
$myObj->lines->qty = $item['qty'];
$myObj->lines->subprice = $item['net_price'];
}
is anyone able to give me a hand? any advice is appreciated
2
Answers
$myObj->lines
should be an array, not a single object. The loop should push a nested associative array onto this array.Or use
array_map()
:Try this