【问题标题】:Get Only Given Field Json_decode() Php仅获取给定字段 Json_decode() Php
【发布时间】:2012-01-17 12:51:05
【问题描述】:

我有以下代码:

<?php 
$url = 'https://www.googleapis.com/shopping/search/v1/public/products?key=keyhere&country=US&q=xbox+360';
$data = curl_init($url);

// Tell curl_exec to return the text instead of sending it to STDOUT
curl_setopt($data, CURLOPT_RETURNTRANSFER, TRUE);

// Don't include return header in output
curl_setopt($data, CURLOPT_HEADER, 0);

$product_result = curl_exec($data);
curl_close($data);

echo $product_result;

$arr = json_decode($product_result, true);

$price_arr = array();
foreach($arr['product'] as $a){
    $price_arr[] = $a['price'];
}


?>

它目前输出如下内容:

{ "kind": "shopping#products", "etag": "\"czKOfew9E3svi7vOBQ3vsAgGZzo/kqYbSV91W3yhAtZVqqRNuag3TY8\"", "id": "tag:google.com,2010:shopping/products", "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=xbox+360&startIndex=1&maxResults=25", "nextLink": "https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=xbox+360&startIndex=26&maxResults=25", "totalItems": 202230, "startIndex": 1, "itemsPerPage": 25, "currentItemCount": 25, "items": [ { "kind": "shopping#product", "id": "tag:google.com,2010:shopping/products/1113342/8881409096328278418", "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/1113342/gid/8881409096328278418", "product": { "googleId": "8881409096328278418", "author": { "name": "Walmart", "accountId": "1113342" }, "creationTime": "2011-11-13T21:18:07.000Z", "modificationTime": "2011-12-08T01:12:50.000Z", "country": "US", "language": "en", "title": "More Facts On Xbox 360 4gb Capabilities", "description": "Xbox 360 4GB ConsoleBuilt-in Wi-Fi: The new Microsoft Xbox 360 console is the only console with 802.11n Wi-Fi built-in for a faster and easier connection to Xbox LIVE. Stream HD movies and TV or download games from Xbox LIVE in 1080p and 5.1 surround sound from anywhere in the house. Compatible with b/g/n networks.Xbox LIVE: Xbox LIVE brings a world of entertainment to you. Instantly watch movies and TV streaming from Netflix. In crystal-clear HD, or download game add-ons like new levels, characters, maps and songs. Easily jump right into online multiplayer fun and play along with friends around the world. Plus, get your favorite music from Last.fm, connect with friends on Facebook and post Tweets on Twitter. Xbox LIVE is your connection to more games, entertainment and fun.Kinect-Ready: Featuring a dedicated, specialized port, this Microsoft Xbox 360 console is built to connect seamlessly with the Kinect Sensor. Kin", "link": "http://www.walmart.com/catalog/product.do?product_id=14917960&sourceid=1500000000000003142040&ci_src=14110944&ci_sku=14917960", "brand": "Microsoft", "condition": "new", "gtin": "00885370138405", "gtins": [ "00885370138405" ], "inventories": [ { "channel": "online", "availability": "inStock", "price": 199.0, "shipping": 7.97, "currency": "USD" } ], "images": [ { "link": "http://i.walmartimages.com/i/p/00/88/53/70/13/0088537013840_500X500.jpg" } ] } }, { "kind": "shopping#product", "id": "tag:google.com,2010:shopping/products/10046/10100715335711953213", "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/10046/gid/10100715335711953213", "product": { "googleId": "10100715335711953213", "author": { "name": "Target", "accountId": "10046" }, "creationTime": "2011-08-23T19:06:38.000Z", "modificationTime": "2011-12-09T22:37:12.000Z", "country": "US", "language": "en", "title": "Xbox 360 4gb Console", "description": "The new Xbox 360 4GB Console. Here today, ready for tomorrow with a brand new, leaner machine. Wi-Fi is built-in for easier connection to the world of entertainment on Xbox LIVE, where HD movies and TV stream in an instant. It s ready for the controller-free experiences of Kinect you don t just play the game, you are the game. Xbox 360 is more games, entertainment and fun. What s In The Box? Xbox 360 4GB S Console Xbox 360 Black Wireless Controller Xbox 360 Composite A/V cable (standard definition) One Month Xbox LIVE Gold Membership 1 year limited warranty on console", "link": "http://www.target.com/p/Xbox-360-4GB-Console/-/A-12885804?ref=tgt_adv_XSG10001&AFID=Froogle_df&LNM=%7C12885804&CPNG=video%20games&ci_src=14110944&ci_sku=12885804", "brand": "Microsoft", "condition": "new", "gtin": "00885370138405", "gtins": [ "00885370138405" ], "inventories": [ { "channel": "online", "availability": "inStock", "price": 199.99, "shipping": 12.85, "currency": "USD" } ], "images": [ { "link": "http://img3.targetimg3.com/wcsstore/TargetSAS/img/p/12/88/12885804.jpg" } ] } }, { "kind": "shopping#product", "id": "tag:google.com,2010:shopping/products/1161353/4408757490534070643", "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/1161353/gid/4408757490534070643", "product": { "googleId": "4408757490534070643", "author": { "name": "Buy.com", "accountId": "1161353" }, "creationTime": "2011-04-24T04:06:32.000Z", "modificationTime": "2011-12-09T06:24:51.000Z", "country": "US", "language": "en", "title": "Xbox 360 System (4GB)", "description": "Product Description The new Xbox 360. Here today, ready for tomorrow with a brand new, leaner machine in an all new black gloss finish. Wi-Fi is built-in for easier connection to the world of entertainment on Xbox LIVE, where HD movies and TV stream in an instant. Kinect Ready Featuring a dedicated, specialized port, the Xbox 360 4GB is built to connect seamlessly with the Kinect Sensor. Kinect brings games and entertainment to life in extraordinary new ways no controller required. Easy to use and instantly fun, Kinect gets everyone off the couch moving, laughing and cheering. See a ball? Kick it. Want to join a friend in the fun? Simply jump in. Even control an HD movie with a wave of your hand. Xbox LIVE Xbox LIVE brings a world of entertainment to you. Instantly watch movies and TV streaming from Netflix. In crystal-clear HD, or download game add-ons like new levels, characters, maps and songs. Easily jump right into online multiplayer fun and play along with friends around the world. Plus, get your favorite music from Last.fm and your Zune Pass, connect with friends on Facebook and post Tweets on Twitter. Xbox LIVE is your connection to more games, entertainment and fun. Looking for more than just a video game system? Just connect your Digital Camera, iPod, Zune, or other MP3 player via USB cable and immediately begin enjoying all your favorite Music and Movies on the TV that your Xbox 360 is hooked up to. It plays DVDs and CDs and you can even download the latest movies and TV shows via Xbox Live. It is a full Windows Media Extender and can connect with other PCs around the house to stream your favorite media. What Does Xbox Live Mean or Do? Join Xbox Live to have full access to the Marketplace where you can download full Games, Demos, Trailers, Movies and more. Voice, Text, and Video chat with friends and even play games together online (Note: multiplayer gaming requires Xbox Live Gold subscription). Xbox 360 Games To Watch Out For Gears of War 3 Call of Duty: Modern Warfare 3 Assassin's Creed: Revelations Tomb Raider Battlefield 3 Deus Ex: Human Revolution Child of Eden Xbox 360 Wireless Controller (black) This award-winning, high-performance wireless controller features a range of up to 30 feet and a battery life of up to 30 hours on two AA batteries. Built-in Wi-Fi The new Xbox 360 is the only console with 802.11n Wi-Fi built-in for a faster and easier connection to Xbox LIVE. Stream HD movies and TV or download games from Xbox LIVE in 1080p and 5.1 surround sound from anywhere in the house. Compatible with b/g/n networks.", "link": "http://clickfrom.buy.com/default.asp?adid=17379&sURL=http%3A%2F%2Fwww.buy.com%2Fprod%2Fxbox-360-system-4gb%2F216907636.html", "brand": "Microsoft", "condition": "new", "gtin": "00885370138405", "gtins": [ "00885370138405" ], "inventories": [ { "channel": "online", "availability": "inStock", "price": 199.99, "shipping": 0.0, "currency": "USD" } ], "images": [ { "link": "http://ak.buy.com/PI/0/1000/216907636.jpg" } ] } }, { "kind": "shopping#product", "id": "tag:google.com,2010:shopping/products/1113342/2934483943836745321", "selfLink": "https://www.googleapis.com/shopping/search/v1/public/products/1113342/gid/2934483943836745321", "product": { "googleId": "2934483943836745321", "author": { "name": "Walmart", "accountId": "1113342" }, "creationTime": "2011-03-25T21:44:47.000Z", "modificationTime": "2011-12-08T01:12:50.000Z", "country": "US", "language": "en", "title": "Xbox 360 250gb Console", "description": "The new Xbox 360 250GB console. It's here today and ready for tomorrow, with a brand-new, leaner machine in an all-new black gloss finish. Wi-Fi is built in for easier connection to the world of entertainment on Xbox LIVE, where HD movies and TV stream in an instant. It's ready for the controller-free experiences of Kinect you don't just play the game; you are the game. Kinect brings games and entertainment to life in extraordinary new ways no controller required. Easy to use and instantly f", "link": "http://www.walmart.com/catalog/product.do?

我如何获得 "price" 的值。例如:"price": 149.0 as 149.0, 65.0 在数组中?

【问题讨论】:

  • price 是单个值。您需要构建它们的数组。
  • 查看修改后的代码。这里有什么问题

标签: php filter json


【解决方案1】:

变化:

$arr = json_decode($product_result);

收件人:

$arr = json_decode($product_result, true);

还有:

$price_arr = array();
foreach($arr as $a){
    $price_arr[] = $a['price'];
}

收件人:

$price_arr = array();
foreach($arr['items'] as $a){
    $price_arr[] = $a['price'];
}

【讨论】:

  • 查看更新的代码,这似乎不会改变任何输出。有什么想法吗?
【解决方案2】:

应该可以做到:

$price_arr = array();
foreach($arr['items'] as $a){
    $price_arr[] = $a['price'];
}

您只需要查看您期望返回的结构,然后进行相应的处理。

【讨论】:

    【解决方案3】:

    以下代码将返回数组中的所有价格。

    <?php 
    $url = 'https://www.googleapis.com/shopping/search/v1/public/products?key=AIzaSyCu8GTvifkYjWOXhOYdbyfOIP8QtVmkYBQ&country=US&q=xbox+360';
    
    $data = curl_init($url);
    curl_setopt($data, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($data, CURLOPT_HEADER, 0);
    
    $product_result = curl_exec($data);
    curl_close($data);
    
    
    $arr = json_decode($product_result, true);
    $prices = array();
    
    foreach ($arr['items'] as $item)
    {
        if (isset($item['product']['inventories'][0]['price']) !== false)
        {
            $prices[] = $item['product']['inventories'][0]['price'];
        }
    }
    
    
    echo '<pre>';
    print_r($prices);
    echo '</pre>';
    ?>
    

    【讨论】:

      猜你喜欢
      • 2018-01-30
      • 1970-01-01
      • 2014-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多