Jump to content

Getting data out of an array


cyberkiller

Recommended Posts

I need to pull data out of google's merchant API. The resulting array I am getting is confusing me though and I am not sure how to pull the data I need from it. How would I get all the "name" fields out of the array or say product titles? Either to just print to screen or save to another variable? I'm currently pulling the data in the json format. Google offers both json and atom. I figured json would be easier? I thought I could just do something like $result['name'] but that doesn't seem to be working.

 

Here's my code, and a snippet of the output.

 

<?php
$result = json_decode(file_get_contents('https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q=digital+camera&alt=json'), true);


echo "<pre>";
var_dump($result);
echo "</pre>";
?>

[code]
array(10) {
  ["kind"]=>
  string(17) "shopping#products"
  ["etag"]=>
  string(57) ""q_Cz93YRTLuwHgwpLYFnNN97DNM/Yat9tcRJVFxzoZmXA_0H3Qp-HuI""
  ["id"]=>
  string(37) "tag:google.com,2010:shopping/products"
  ["selfLink"]=>
  string(125) "https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=digital+camera&alt=json&startIndex=1&maxResults=25"
  ["nextLink"]=>
  string(126) "https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=digital+camera&alt=json&startIndex=26&maxResults=25"
  ["totalItems"]=>
  int(833935)
  ["startIndex"]=>
  int(1)
  ["itemsPerPage"]=>
  int(25)
  ["currentItemCount"]=>
  int(25)
  ["items"]=>
  array(25) {
    [0]=>
    array(4) {
      ["kind"]=>
      string(16) "shopping#product"
      ["id"]=>
      string(66) "tag:google.com,2010:shopping/products/1113342/17031817732574057954"
      ["selfLink"]=>
      string(103) "https://www.googleapis.com/shopping/search/v1/public/products/1113342/gid/17031817732574057954?alt=json"
      ["product"]=>
      array(15) {
        ["googleId"]=>
        string(20) "17031817732574057954"
        ["author"]=>
        array(2) {
          ["name"]=>
          string(7) "Walmart"
          ["accountId"]=>
          string(7) "1113342"
        }
        ["creationTime"]=>
        string(24) "2011-07-07T18:51:04.000Z"
        ["modificationTime"]=>
        string(24) "2011-10-16T21:59:30.000Z"
        ["country"]=>
        string(2) "US"
        ["language"]=>
        string(2) "en"
        ["title"]=>
        string(66) "Canon Powershot Sx30-is Black 14.1mp Digital Camera W/ 35x Optical"
        ["description"]=>
        string(187) "Canon PowerShot SX30-IS 14MP Black Digital Camera:14.1 megapixel resolutionDelivers excellent picture quality24-840mm wide-angle zoom lensLets you get up close with the 35x optical zoom2."
        ["link"]=>
        string(117) "http://www.walmart.com/ip/Canon-14.1-MP-SX30/14972590?sourceid=1500000000000003142050&ci_src=14110944&ci_sku=14972590"
        ["brand"]=>
        string(5) "Canon"
        ["condition"]=>
        string(3) "new"
        ["gtin"]=>
        string(14) "00013803127348"
        ["gtins"]=>
        array(1) {
          [0]=>
          string(14) "00013803127348"
        }
        ["inventories"]=>
        array(1) {
          [0]=>
          array(5) {
            ["channel"]=>
            string(6) "online"
            ["availability"]=>
            string(7) "inStock"
            ["price"]=>
            float(349.99)
            ["shipping"]=>
            float(2.97)
            ["currency"]=>
            string(3) "USD"
          }
        }
        ["images"]=>
        array(1) {
          [0]=>
          array(1) {
            ["link"]=>
            string(71) "http://i.walmartimages.com/i/p/00/01/38/03/12/0001380312734_500X500.jpg"
          }
        }
      }
    }
    [1]=>
    array(4) {
      ["kind"]=>
      string(16) "shopping#product"
      ["id"]=>
      string(65) "tag:google.com,2010:shopping/products/3622330/1688520904457034451"
      ["selfLink"]=>
      string(102) "https://www.googleapis.com/shopping/search/v1/public/products/3622330/gid/1688520904457034451?alt=json"
      ["product"]=>
      array(15) {
        ["googleId"]=>
        string(19) "1688520904457034451"
        ["author"]=>
        array(2) {
          ["name"]=>
          string(11) "Apple Store"
          ["accountId"]=>
          string(7) "3622330"
        }
        ["creationTime"]=>
        string(24) "2010-10-21T20:31:02.000Z"
        ["modificationTime"]=>
        string(24) "2011-10-14T21:10:06.000Z"
        ["country"]=>
        string(2) "US"
        ["language"]=>
        string(2) "en"
        ["title"]=>
        string(39) "Canon PowerShot SX130 IS Digital Camera"
        ["description"]=>
        string(532) "Record In Stereo Sound, Then Play Back Your Videos Instantly On You Computer. The Sx130 Is Is All About Creativity, With Fun New Scene Modes Like Miniature Effect For Movies, And Fisheye Effect For Artistically Distorted Stills. While You're Having Fun Creating, The Camera's Smart Flash Exposure And Advanced Smart Auto Systems Are Ensuring That Every Image Is The Best It Can Be. Add The Digic 4 Image Processor, 12.1 Megapixels And Canon's Optical Image Stabilizer, And You've Got The Ideal Camera For Making The Good Times Last."
        ["link"]=>
        string(112) "http://store.apple.com/us/go/product/H2807ZM/A?aosid=p228&affiliateID=10262&cid=AOS-US-SHOPIP-Google&site=Google"
        ["brand"]=>
        string(5) "Canon"
        ["condition"]=>
        string(3) "new"
        ["gtin"]=>
        string(14) "00013803127386"
        ["gtins"]=>
        array(1) {
          [0]=>
          string(14) "00013803127386"
        }
        ["inventories"]=>
        array(1) {
          [0]=>
          array(4) {
            ["channel"]=>
            string(6) "online"
            ["availability"]=>
            string(7) "inStock"
            ["price"]=>
            float(229.95)
            ["currency"]=>
            string(3) "USD"
          }
        }
        ["images"]=>
        array(1) {
          [0]=>
          array(1) {
            ["link"]=>
            string(177) "http://storeimages.apple.com/1819/as-images.apple.com/is/image/AppleInc/H2807?wid=600&hei=600&fmt=jpeg&qlt=95&op_sharpen=0&resMode=bicub&op_usm=0.5,0.5,0,0&iccEmbed=0&layer=comp"
          }
        }
      }
    }
    [2]=>
    array(4) {
      ["kind"]=>
      string(16) "shopping#product"
      ["id"]=>
      string(66) "tag:google.com,2010:shopping/products/1113342/13850367466326274615"
      ["selfLink"]=>
      string(103) "https://www.googleapis.com/shopping/search/v1/public/products/1113342/gid/13850367466326274615?alt=json"
      ["product"]=>
      array(15) {
        ["googleId"]=>
        string(20) "13850367466326274615"
        ["author"]=>
        array(2) {
          ["name"]=>
          string(7) "Walmart"
          ["accountId"]=>
          string(7) "1113342"
        }
        ["creationTime"]=>
        string(24) "2011-07-04T19:48:28.000Z"
        ["modificationTime"]=>
        string(24) "2011-10-16T21:59:30.000Z"
        ["country"]=>
        string(2) "US"
        ["language"]=>
        string(2) "en"
        ["title"]=>
        string(67) "Canon Powershot Sx130-is Black 12.1mp Digital Camera W/ 12x Optical"
        ["description"]=>
        string(599) "Canon PowerShot SX130-IS 12.1MP Digital Camera:12.1-megapixel resolutionDelivers excellent picture qualityWide-angle zoom lensLets you get up close with the 12x optical zoom3.0" widescreen LCD displayEnjoy easy focus selection and photo viewingShoot HD videoRecord movies in 720p HD format on the Canon black 12.1MP digital cameraSmart Flash Exposure PowerShot SX130-IS determines the correct flashUseful new scene modesMiniature Effect for movies and Fisheye for capturing imagesSmart AutoSelects the proper settings based on 28 predefined shooting situationsMulti-format memory slotCanon black 12."
        ["link"]=>
        string(219) "http://www.walmart.com/ip/Kodak-Playsport-Video-Camera-Value-Bundle-w-Bonus-4GB-Memory-Card-Camera-Yellow-Float-Strap-Rechargeable-Battery-Charger/14972582?sourceid=1500000000000003142050&ci_src=14110944&ci_sku=14972582"
        ["brand"]=>
        string(5) "Canon"
        ["condition"]=>
        string(3) "new"
        ["gtin"]=>
        string(14) "00013803127386"
        ["gtins"]=>
        array(1) {
          [0]=>
          string(14) "00013803127386"
        }
        ["inventories"]=>
        array(1) {
          [0]=>
          array(5) {
            ["channel"]=>
            string(6) "online"
            ["availability"]=>
            string(7) "inStock"
            ["price"]=>
            float(179.99)
            ["shipping"]=>
            float(2.97)
            ["currency"]=>
            string(3) "USD"
          }

Link to comment
Share on other sites

Well, how you get the values you are looking for depends on which ones they are since it is a mulch-dimensional array. You simply need to traverse down to the element you are looking for.

 

This should get you the names if I am reading the structure correct:

 

$names = array();
foreach($result['items'] as $item)
{
    foreach($item['product'] as $product)
    {
        foreach($product['author'] as $author)
        {
            $names[] = $author['name'];
        }
    }
}

print_r($names);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.