Array reading with JSON

Asked

Viewed 103 times

-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 I made. It is 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
        }
      ]
    }
  ]
}

However I am getting the Array shown above.

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

I’m trying to impress with:

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

but I’m not getting it. What would be the right way? I keep getting the Trying to get Property of non-object error on the FOREACH line.

1 answer

0


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

The right thing would be:

// Percorre a lista de pedidos:
foreach($retorno["xml"]->orders as $pedido)
{
    echo $pedido->customer->name;
    echo $pedido->customer->email;
}
  • I’m getting the Trying to get Property of non-object error on the foreach line :/ already tried to give $return = json_decode($return); and it still didn’t work....

  • My code made that mistake?

  • Yes! ): I don’t know what to do. The $return is coming from a GET request I’m making

  • I edited the answer. See if anything changes.

  • The error remains the same.

  • Ok, edit the question and add your JSON and how you are using the json_decode.

  • All right, take a look there :)

  • @Inovecommerce Given the data you put, my first solution should work: https://ideone.com/JjfGqp. Either you didn’t put the data correctly in the question, or you did something different. I recommend that you rewrite your question and detail better what the problem is. For example, there is a $this->get_b2w($url) in your question that is completely lost, without any explanation as to what this method is.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.