skip to Main Content

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


  1. $myObj->lines should be an array, not a single object. The loop should push a nested associative array onto this array.

    $myObj->lines = [];
    foreach($result['items_list'] as $item) {
        $myObj->lines[] = [
            'desc' =>  $item['name'],
            'qty' => $item['qty'],
            'subprice' => $item['net_price'],
            ...
        ];
    }
    

    Or use array_map():

    $myObj->lines = array_map(function($item) {
        return [
            'desc' =>  $item['name'],
            'qty' => $item['qty'],
            'subprice' => $item['net_price'],
            ...
        ];
    }, $result['items_list']);
    
    Login or Signup to reply.
  2. Try this

    $finaArr= []:
    foreach($result['items_list'] as $item) {
    $tmp=array("qty"=>$item["qty"],
    "subprice"=> $item["price"],
     "tva_tx"=>$item["tax"], 
     "fk_product"=>$item["name"], 
     "remise_percent"=>$item["remise"],
     "date_start"=>$item["startDate"], 
     "date_end"=>$item["endDate"], 
     "info_bits"=>$item["info"]);
    array_push($finaArr,$tmp):
    }
    return $finaArr;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search