2015-01-05 2 views
0

아래는 json 응답이며 shipping_address 데이터를받는 데 문제가 있습니다.json 배열에서 데이터 가져 오기 - 정의되지 않은 색인 오류

$content = file_get_contents($url); 
$json = json_decode($content, true); 
foreach($json['orders'] as $order){ 
    echo "<td>".$order['id']."</td>"; 
} 

을하지만이 같은 shipping_address에게 데이터를 얻을 때 :

{ 
    "orders": [{ 
     "created_at": "2015-01-04T02:20:03+01:00", 
     "financial_status": "paid", 
     "id": 384536708, 
     "total_price": "45.00", 
     "line_items": [{ 
      "fulfillment_service": "manual", 
      "fulfillment_status": null, 
      "gift_card": false, 
      "grams": 0, 
      "price": "45.00", 
      "quantity": 1, 
      "requires_shipping": true, 
      "taxable": true, 
      "title": "test", 
      "vendor": "test", 
      "name": "test", 
      "properties": [], 
      "product_exists": true, 
      "fulfillable_quantity": 1, 
      "tax_lines": [] 
     }], 
     "shipping_address": { 
      "address1": "Street 20", 
      "address2": "", 
      "city": "City", 
      "company": "", 
      "country": "USA", 
      "first_name": "Name", 
      "last_name": "Surname", 
      "latitude": 45.000000, 
      "longitude": 10.000000, 
      "phone": "", 
      "province": "", 
      "zip": "12345", 
      "name": "Name Surname", 
      "country_code": "US", 
      "province_code": null 
     } 
    }, [...] 

이 코드로 주문을 얻을 수있는 그런

foreach ($json['shipping_address'] as $sa) { 
    echo "<td>".$sa['name']."</td>"; 
} 

를 내가 얻을 : 공지 사항 : 정의되지 않은 색인 : shipping_address경고 : foreach()에 대해 잘못된 인수가 제공되었습니다.

타겟팅 배열에 문제가 있습니까?

+0

디버그,'후'$ json = json_decode ($ content, true);'이 라인 코드. – Girish

답변

0

그것은해야 -

foreach ($json['orders']['shipping_address'] as $sa) { 

이 시도 -이 시도

foreach($json['orders'] as $order){ 
    echo "<td>".$order['id']."</td>"; 
    echo "<td>".$order['shipping_address']['name']."</td>"; 
} 
+0

이전 대답과 같은 오류입니다. 어떻게 든 foreach 루프에서 둘 다 호출 할 수 있습니까? – jakob

+0

답변이 업데이트되었습니다. 이것으로 둘 다 같은 루프에 접근 할 수 있습니다. –

0
foreach ($json['orders']['shipping_address'] as $sa) { 

    echo "<td>".$sa['name']."</td>"; 
} 
+0

이것을 추가 할 때도 같은 오류가 발생합니다. – jakob

0

을 : 인 print_r ($의 JSON)`와

$content = file_get_contents($url); 
$json = json_decode($content, true); 

foreach($json as $orders){ 
    foreach($orders as $odarr){ 
     foreach($odarr['shipping_address'] as $shipadd){ 
      echo $shipadd['address1']; 
     } 
    } 
}