Array reading with JSON

-2

I have the following array:

Array
(
    [xml] => {
        "orders": [ 
            {
                "code":"PedidoTeste-1508156986545",
                "channel":"PedidoTeste",
                "placed_at":"2017-10-16T10:29:46-02:00",
                "updated_at":"2017-10-16T10:29:46-02:00",
                "total_ordered":155.0,
                "interest":0.0,
                "discount":0.0,
                "shipping_cost":0.0,
                "shipping_method":"Correios PAC",
                "estimated_delivery":"2018-02-10T22:00:00-02:00",
                "estimated_delivery_shift":null,

                "shipping_address": {

                    "full_name":"Denis Moura",
                    "street":"Rua Sacadura Cabral",
                    "number":"130",
                    "detail":"",
                    "neighborhood":"Centro",
                    "city":"Rio de Janeiro",
                    "region":"RJ",
                    "country":"BR",
                    "postcode":"20081262",
                    "phone":"21 3722-3902",
                    "secondary_phone":null

                },

                "billing_address": {

                    "full_name":"Denis Moura",
                    "street":"Rua Sacadura Cabral",
                    "number":"130",
                    "detail":"",
                    "neighborhood":"Centro",
                    "city":"Rio de Janeiro",
                    "region":"RJ",
                    "country":"BR",
                    "postcode":"20081262",
                    "phone":"21 3722-3902",
                    "secondary_phone":null

                },

                "customer": {
                    "name":"Denis Moura",
                    "email":"[email protected]",
                    "date_of_birth":"1998-01-25",
                    "gender":"male",
                    "vat_number":"78732371683",
                    "phones":[
                        "21 3722-3902"
                    ]
                },

                "items":[
                    {
                        "id":"CAPR-5001778435-",
                        "product_id":"CA-1172191395-13500",
                        "name":"Carrinho Auxiliar CA-40",
                        "qty":1,
                        "original_price":162.8,
                        "special_price":155.0
                    }
                ],    
                "status": {
                    "code":"book_product",
                    "label":"Pagamento Pendente (SkyHub)",
                    "type":"NEW"
                },    
                "sync_status":"NOT_SYNCED",
                "invoices":[],
                "shipments":[],
                "payments":[ 
                    {
                        "method":"CREDIT_CARD",
                        "description":"Cartao",
                        "parcels":1,"value":155.0
                    }
                ]    
            },    

    [httpcode] => 200
)

This array came from a GET request that I made. It gets stored in $ return.

$url = "https://api.skyhub.com.br/orders";
$retorno = $this->get_b2w($url);

I actually expected $ return to come as a string, to give:

$retorno = json_decode($retorno);

And receive the following JSON:

{
  "orders": [
    {
      "code": "PedidoTeste-1508156986545",
      "channel": "PedidoTeste",
      "placed_at": "2017-10-16T10:29:46-02:00",
      "updated_at": "2017-10-16T10:29:46-02:00",
      "total_ordered": 155,
      "interest": 0,
      "discount": 0,
      "shipping_cost": 0,
      "shipping_method": "Correios PAC",
      "estimated_delivery": "2018-02-10T22:00:00-02:00",
      "estimated_delivery_shift": null,
      "shipping_address": {
        "full_name": "Denis Moura",
        "street": "Rua Sacadura Cabral",
        "number": "130",
        "detail": "",
        "neighborhood": "Centro",
        "city": "Rio de Janeiro",
        "region": "RJ",
        "country": "BR",
        "postcode": "20081262",
        "phone": "21 3722-3902",
        "secondary_phone": null
      },
      "billing_address": {
        "full_name": "Denis Moura",
        "street": "Rua Sacadura Cabral",
        "number": "130",
        "detail": "",
        "neighborhood": "Centro",
        "city": "Rio de Janeiro",
        "region": "RJ",
        "country": "BR",
        "postcode": "20081262",
        "phone": "21 3722-3902",
        "secondary_phone": null
      },
      "customer": {
        "name": "Denis Moura",
        "email": "[email protected]",
        "date_of_birth": "1998-01-25",
        "gender": "male",
        "vat_number": "78732371683",
        "phones": [
          "21 3722-3902"
        ]
      },
      "items": [
        {
          "id": "CAPR-5001778435-",
          "product_id": "CA-1172191395-13500",
          "name": "Carrinho Auxiliar CA-40",
          "qty": 1,
          "original_price": 162.8,
          "special_price": 155
        }
      ],
      "status": {
        "code": "payment_received",
        "label": "Aprovado (SkyHub)",
        "type": "APPROVED"
      },
      "sync_status": "NOT_SYNCED",
      "invoices": [],
      "shipments": [],
      "payments": [
        {
          "method": "CREDIT_CARD",
          "description": "Cartao",
          "parcels": 1,
          "value": 155
        }
      ]
    }
  ]
}

But I'm getting the Array shown above.

I'm trying to print the name and email of the customer of each order (in case this array only has 1 order, but could have several).

I'm trying to start with:

foreach($retorno["xml"]->orders as $pedido)
{
   echo $pedido->customer->name;
   echo $pedido->customer->email;
}
return;

But I can not. What would be the correct form? I'm getting the error Trying to get property of non-object in the FOREACH line.

    
asked by anonymous 17.10.2017 / 14:22

1 answer

0

If you need to go through the requests, then foreach must iterate through the list of requests, not clients. Probably the "failure" you mentioned is an error saying that the array $retorno->xml->orders does not have the customers attribute, so only then should you be able to identify what is wrong the error message told you this).

The correct one would be:

// Percorre a lista de pedidos:
foreach($retorno["xml"]->orders as $pedido)
{
    echo $pedido->customer->name;
    echo $pedido->customer->email;
}
    
17.10.2017 / 14:28