2013-03-25 40 views
13

mã PHP của tôi:Làm thế nào để lặp qua mảng JSON sử dụng PHP

$obj = json_decode($data); 
print $obj->{'name'}; 

Trong khi nó hoạt động cho phi mảng, tôi không thể cho cuộc đời của tôi tìm ra cách để in tất cả các giá trị trong phạm vi " Bài đánh giá "Array.

Những gì tôi muốn làm là để lặp qua phản ứng này, có lẽ với forreach(), kết quả là một danh sách có chứa các đánh giá và trích cho mỗi lần xem xét trong phản ứng.

Mọi hướng dẫn/chỉ đường được đánh giá cao ..

Dưới đây là JSON tôi đang làm việc. (đó là phản hồi từ API Yelp).

{ 
    "is_claimed": true, 
    "rating": 4.5, 
    "mobile_url": "http://m.yelp.com/biz/economy-paint-and-collision-riverside", 
    "rating_img_url": "http://s3-media2.ak.yelpcdn.com/assets/2/www/img/99493c12711e/ico/stars/v1/stars_4_half.png", 
    "review_count": 19, 
    "name": "Economy Paint & Collision", 
    "snippet_image_url": "http://s3-media3.ak.yelpcdn.com/photo/ZOzoahw0Go_DEPLvxCaP_Q/ms.jpg", 
    "rating_img_url_small": "http://s3-media2.ak.yelpcdn.com/assets/2/www/img/a5221e66bc70/ico/stars/v1/stars_small_4_half.png", 
    "url": "http://www.yelp.com/biz/economy-paint-and-collision-riverside", 
    "reviews": [ 
     { 
      "rating": 3, 
      "excerpt": "The Good:\nDennis quoted me a price over the phone about 1 month before I took my wifes 2010 Escalade in for repairs and when I took it in he gave me the...", 
      "time_created": 1357010247, 
      "rating_image_url": "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/34bc8086841c/ico/stars/v1/stars_3.png", 
      "rating_image_small_url": "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/902abeed0983/ico/stars/v1/stars_small_3.png", 
      "user": { 
       "image_url": "http://s3-media3.ak.yelpcdn.com/photo/mIsU7ugYd88lLA-XL2q1Cg/ms.jpg", 
       "id": "V9MDZvEBv-tBTF4YIoc7mg", 
       "name": "Sydney H." 
      }, 
      "rating_image_large_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/e8b5b79d37ed/ico/stars/v1/stars_large_3.png", 
      "id": "HfOhzLIlJoUKSKU8euclqA" 
     }, 
     { 
      "rating": 5, 
      "excerpt": "Dennis and his team did an amazing job on the roof of my fiancee's 2002 Acura RSX after years of living by the beach in San Francisco had mostly rusted...", 
      "time_created": 1354741952, 
      "rating_image_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png", 
      "rating_image_small_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png", 
      "user": { 
       "image_url": "http://s3-media3.ak.yelpcdn.com/photo/ZOzoahw0Go_DEPLvxCaP_Q/ms.jpg", 
       "id": "kOqCnCjYn0EbAhtH1tfjcw", 
       "name": "Jason H." 
      }, 
      "rating_image_large_url": "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png", 
      "id": "YzZg1LX6zeRaurq9tYUcMw" 
     }, 
     { 
      "rating": 5, 
      "excerpt": "It's been a year since I had my car painted here, and I gotta say: It still looks just as good as it did when I first picked it up. You would never know...", 
      "time_created": 1361043626, 
      "rating_image_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png", 
      "rating_image_small_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png", 
      "user": { 
       "image_url": "http://s3-media1.ak.yelpcdn.com/photo/58coTtu1x5riHSgFEAQsfw/ms.jpg", 
       "id": "kVrW3138d5VL-AZ97wFF4A", 
       "name": "Jeanne M." 
      }, 
      "rating_image_large_url": "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png", 
      "id": "r5WtlQVMXiIMBR6S3N7RZw" 
     } 
    ], 
    "phone": "9517870227", 
    "snippet_text": "Dennis and his team did an amazing job on the roof of my fiancee's 2002 Acura RSX after years of living by the beach in San Francisco had mostly rusted...", 
    "image_url": "http://s3-media3.ak.yelpcdn.com/bphoto/kodoEcmgHRG61pPaWRndbw/ms.jpg", 
    "categories": [ 
     [ 
      "Body Shops", 
      "bodyshops" 
     ], 
     [ 
      "Auto Repair", 
      "autorepair" 
     ] 
    ], 
    "display_phone": "+1-951-787-0227", 
    "rating_img_url_large": "http://s3-media4.ak.yelpcdn.com/assets/2/www/img/9f83790ff7f6/ico/stars/v1/stars_large_4_half.png", 
    "id": "economy-paint-and-collision-riverside", 
    "is_closed": false, 
    "location": { 
     "city": "Riverside", 
     "display_address": [ 
      "2548 Rubidoux Blvd", 
      "Riverside, CA 92509" 
     ], 
     "geo_accuracy": 8, 
     "postal_code": "92509", 
     "country_code": "US", 
     "address": [ 
      "2548 Rubidoux Blvd" 
     ], 
     "coordinate": { 
      "latitude": 34.0132437, 
      "longitude": -117.3923804 
     }, 
     "state_code": "CA" 
    } 
} 

Trả lời

17

Có thể bạn đang gặp sự cố vì bài đánh giá là một mảng và bạn đang cố gắng truy cập nó dưới dạng đối tượng JSON.

$obj = json_decode($data, TRUE); 
for($i=0; $i<count($obj['reviews']); $i++) { 
    echo "Rating is " . $obj['reviews'][$i]["rating"] . " and the excerpt is " . $obj['reviews'][$i]["excerpt"] . "<BR>"; 
} 
+0

bạn đẹp, đẹp người đàn ông THANK YOU !!! Đã biến mất nhanh chóng cho đến khi câu trả lời này – user2170611

+0

làm cách nào để bạn nhận được những thứ thấp hơn trong dữ liệu, như người dùng "image_url"? – internetgho5t

1

Tôi không chắc chắn chính xác bạn muốn nhưng tôi đoán bạn muốn in nó chỉ để gỡ lỗi ngay bây giờ. Bạn có thể thử với print_r($obj);var_dump($obj); - họ phải in cái gì đó, đặc biệt là var_dump(). Khi bạn xem dữ liệu, bạn có thể dễ dàng chỉnh sửa hoạt động một chút, vì vậy bạn có thể làm ví dụ print_r($obj->reviews) hoặc print_r($obj['reviews']), tùy thuộc nếu $obj là đối tượng hoặc mảng.

+0

Cảm ơn bạn đã trả lời, những gì tôi đang cố gắng chỉ đơn giản là in/echo xếp hạng và trích đoạn cho tất cả các thực thể trong mảng đánh giá. Tôi bị mất về cách in/tiếng vang dữ liệu này một mình, mà không cần mọi thứ khác – user2170611

0

Bạn có thể sử dụng var_dump hoặc print_r.

<?php 
$decodedJSON = json_decode($jsonData); 

// Put everyting to the screen with var_dump; 
var_dump($decodedJSON); 

// With print_r (useful for arrays); 
print_r($decodedJSON); 

// List just review ratings with foreach; 
foreach($decodedJSON['reviews'] as $review){ 
    echo $review['rating']; 
} 
?> 
Các vấn đề liên quan