doujiang2020 2015-01-30 04:24
浏览 51
已采纳

PHP变量的JSON KEY / VALUE

I'll get right to the point.

I'm trying to save data retrieved from an API using the following:

$i=0;
foreach($results->results as $product){
    $products[$i]['pid']            = $product->listing_id;
    $products[$i]['title']          = $product->title;
    $products[$i]['url']            = $product->url;
    $products[$i]['price']          = $product->price;
    $products[$i]['quantity']       = $product->quantity;
    $products[$i]['endtime']        = $product->ending_tsz;
$products[$i]['thumb']          = $product->results.Images[url_75x75];

Here is the JSON request:

{
   "count":163,
   "results":[
      {
     "listing_id":118973432,
     "state":"active",
     "user_id":27549667,
     "category_id":68894752,
     "title":"Funny I Love You Valentines Card - I Heart you Even More then a Nerd Loves Starwars - Adult Funny Humor Greeting Cards",
     "description":"You know how much Nerds be loving the Star Wars , this Valentines day show the one you love that you love them even more then that - The perfect greeting card for that special someone!

GUARANTEED CHRISTMAS DELIVERY - Place your orders by 12\/19 and get it by Christmas Eve - Upgraded Two Shipping available starting at $5, just contact us to upgrade

Details:

- Outside Message: I love you even more then a nerd loves Starwars
- Inside Message: Blank Inside
- Size A7 (5x7)
- Matching White Envelope included 
- Packaged in a cello sleeve for protection in transit

We will gladly ship to your recipient! Just specify a ship to address and any special message in "notes to seller" at checkout.  Need a Custom Card, Magnet or keychain, we do that too!

Shipping:
US S&H is $2-Ships within 1 business day. Please allow 2-5 business days for delivery.
Intl S&H is $4 - Ships within 1 business day - Please allow 7-10 business days for delivery.

Buy MORE and SAVE on shipping! Express shipment is available to most US cities contact us for price and details.

For More Rude, crude and downright funny greeting cards, novelty gifts and prints, visit our Etsy store and browse a large selection single cards, greeting card sets, prints and more totally edgy, racy, adult, crass, lude, perverted and all things nasty!
imjustsayininc.etsy.com",
     "creation_tsz":1422565585,
     "ending_tsz":1432929985,
     "original_creation_tsz":1356746393,
     "last_modified_tsz":1422565585,
     "price":"4.00",
     "currency_code":"USD",
     "quantity":3,
     "tags":[
        "funny card",
        "greeting card",
        "novelty card",
        "mature card",
        "adult card",
        "cards",
        "funny cards",
        "relationship card",
        "love card",
        "i love you card",
        "valentines cards",
        "funny valentine",
        "valentines day card"
     ],
     "category_path":[
        "Paper Goods",
        "Cards",
        "Valentine"
     ],
     "category_path_ids":[
        69150367,
        69152963,
        68894752
     ],
     "materials":[
        "Papaer",
        "Ink",
        "Envelope"
     ],
     "shop_section_id":12656719,
     "featured_rank":null,
     "state_tsz":1421921498,
     "url":"https:\/\/www.etsy.com\/listing\/118973432\/funny-i-love-you-valentines-card-i-heart?utm_source=massetsy&utm_medium=api&utm_campaign=api",
     "views":764,
     "num_favorers":102,
     "shipping_template_id":null,
     "processing_min":1,
     "processing_max":1,
     "who_made":"i_did",
     "is_supply":"false",
     "when_made":"2010_2015",
     "is_private":false,
     "recipient":"unisex_adults",
     "occasion":"valentines",
     "style":null,
     "non_taxable":false,
     "is_customizable":true,
     "is_digital":false,
     "file_data":"",
     "language":"en-US",
     "has_variations":false,
     "used_manufacturer":false,
     "Images":[
        {
           "listing_image_id":410447372,
           "hex_code":"C1AFB1",
           "red":193,
           "green":175,
           "blue":177,
           "hue":353,
           "saturation":9,
           "brightness":75,
           "is_black_and_white":false,
           "creation_tsz":1356746394,
           "listing_id":118973432,
           "rank":1,
           "url_75x75":"https:\/\/img0.etsystatic.com\/015\/0\/7566894\/il_75x75.410447372_sjo4.jpg",
           "url_170x135":"https:\/\/img0.etsystatic.com\/015\/0\/7566894\/il_170x135.410447372_sjo4.jpg",
           "url_570xN":"https:\/\/img0.etsystatic.com\/015\/0\/7566894\/il_570xN.410447372_sjo4.jpg",
           "url_fullxfull":"https:\/\/img0.etsystatic.com\/015\/0\/7566894\/il_fullxfull.410447372_sjo4.jpg",
           "full_height":737,
           "full_width":600
        },
        {
           "listing_image_id":402927400,
           "hex_code":"897761",
           "red":137,
           "green":119,
           "blue":97,
           "hue":33,
           "saturation":29,
           "brightness":53,
           "is_black_and_white":false,
           "creation_tsz":1356746394,
           "listing_id":118973432,
           "rank":2,
           "url_75x75":"https:\/\/img0.etsystatic.com\/003\/0\/7566894\/il_75x75.402927400_e80x.jpg",
           "url_170x135":"https:\/\/img0.etsystatic.com\/003\/0\/7566894\/il_170x135.402927400_e80x.jpg",
           "url_570xN":"https:\/\/img0.etsystatic.com\/003\/0\/7566894\/il_570xN.402927400_e80x.jpg",
           "url_fullxfull":"https:\/\/img0.etsystatic.com\/003\/0\/7566894\/il_fullxfull.402927400_e80x.jpg",
           "full_height":800,
           "full_width":986
        },
        {
           "listing_image_id":402923983,
           "hex_code":"C8C1C0",
           "red":200,
           "green":193,
           "blue":192,
           "hue":8,
           "saturation":4,
           "brightness":78,
           "is_black_and_white":false,
           "creation_tsz":1356746394,
           "listing_id":118973432,
           "rank":3,
           "url_75x75":"https:\/\/img1.etsystatic.com\/005\/0\/7566894\/il_75x75.402923983_snri.jpg",
           "url_170x135":"https:\/\/img1.etsystatic.com\/005\/0\/7566894\/il_170x135.402923983_snri.jpg",
           "url_570xN":"https:\/\/img1.etsystatic.com\/005\/0\/7566894\/il_570xN.402923983_snri.jpg",
           "url_fullxfull":"https:\/\/img1.etsystatic.com\/005\/0\/7566894\/il_fullxfull.402923983_snri.jpg",
           "full_height":440,
           "full_width":640
        }
     ]
  }
   ],
   "params":{
  "limit":"1",
  "offset":0,
  "page":null,
  "shop_id":"imjustsayininc",
  "keywords":null,
  "sort_on":"created",
  "sort_order":"down",
  "min_price":null,
  "max_price":null,
  "color":null,
  "color_accuracy":0,
  "tags":null,
  "category":null,
  "translate_keywords":"false",
  "include_private":0
   },
   "type":"Listing",
   "pagination":{
  "effective_limit":1,
  "effective_offset":0,
  "next_offset":1,
  "effective_page":1,
  "next_page":2
   }
    }

What I'm trying to do is pull all values from the image field where it shows an image URL (url_75x75,url_170x135,url_570xN & url_fullxfull).

I seem to be stuck only on the importing of the images. They seems to be in an array and I can't figure out how to implement pulling them like I do the rest of the content.

I've spent roughly 3 days searching, trying and failing at doing this.

I was successful when I had the Json request split up into separate requests but recently learned I could include images into one JSON request which is the method I am currently trying.

How can I do this?

I'm pretty new to PHP and I'm even newer at JSON so please be descriptive as possible and try not to overload me with complex ways I could do this, thank you.

  • 写回答

1条回答 默认 最新

  • dpf56454 2015-01-30 04:40
    关注

    results is an array of objects, so is Images

    I didn't test it but I think something like this should be able to retrieve the first image of the first result:

    $product->results[0]->Images[0]->url_75x75
    

    In your code, this is obviously wrong:

    $product->results.Images[url_75x75]
    

    Javascript with PHP syntax mixed together? ;-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏