2014-06-28 85 views
-3

我想使用下面列出的php代碼來讀取json文件,但我無法將這些值分配給本地變量。無法讀取json文件並將值分配給本地varibales

有人可以幫我解釋我做錯了什麼?

<?php 

$file = "http://localsurch.com/deals2.txt"; 
$response = json_decode(file_get_contents($file), true); 

//print_r($response); 

foreach ($response as $mydeal) 
{ 
    $category    = $mydeal->category->name; 
$title    = $mydeal->websiteTitle; 
$finePrint    = $mydeal->finePrint; 
$imageURL  = $mydeal->imageURL; 
$merchant   = $mydeal->merchant->displayName; 
$streetaddress1  = $mydeal->redemptionLocations->addressStreet1; 

} 

?> 

陣列 ( [日期] => 28君14 5.46.34.871 PM [優惠] =>數組 ( [0] =>數組 ( [類別] =>數組 ( [名稱] =>修理&服務 [路徑] =>數組 ( [0] =>汽車 [1] =>修理&服務 )

    ) 

       [websiteTitle] => Three Full-Service Oil Changes, Tire Rotations, and More 
       [description] => <p>Since 1988, <a href="http://www.planetsupersaver.com/"target="_blank"> Planet Super Saver</a> has been saving members thousands of dollars on their automotive maintenance and repairs nationwide. Their goal is to unite you with trustworthy, top-notch service centers in your area at a huge savings. The service center's goal is to introduce themselves to you with the hopes you'll become a long-term customer through their honesty and professional service you can depend on.</p> 
  • $ 30($ 179值)的汽車維修包
  • 包括三個完整地原油變化中,兩個輪胎的旋轉,診斷和檢查
  • 預防保健可能意味着節省大量的道路
  • 高效,友好的專業人士把工作做對
   [finePrint] => <ul><li><b>Online redemption required at planetsupersaver.com; a punch card will be mailed within 5 business days</b></li> 

  • 衝卡從贖回日期起有效期爲1年
  • 任命是必需的,並視情況而定;欲瞭解更多信息,請致電Planet Super Saver客戶支持電話:480-921-8282
  • 24小時的商戶取消/重新安排政策適用;憑單予沒收
  • 穿孔卡片是由同一人或家庭擁有的車輛之間的轉讓,並在多次訪問
  • 僅在購買時選擇位置的有效使用
  • 不能與任何其他優惠同時使用或促銷
  • 買儘可能多的,你喜歡;發送儘可能多的禮物
  • 完整的換油包括多達5夸脫的油,潤滑油和過濾器。額外的石英和合成油或柴油可額外收費
  • 一些要求冷媒和冷卻液的服務需要支付額外費用
  • $ 4。50處置費,稅收和小費不包括
  • 可用於使用購買後立即
  • PROMOTIONAL值到期180天,從購買日期
  • PAID值到期從購買日起5年
  • [ASIN] = > B00LBL758W [IMAGEURL] => /圖像/ G/01 /餘燼/交易/ c617b334d1893eae7cbc94301fab538c5880b3e24f95669d10ad118fe38eaadc [商家] =>數組 ( [顯示名] =>行星超級節電器 )

       [offerEndTime] => 1411714800000 
           [options] => Array 
            (
             [0] => Array 
              (
               [title] => 7111 Sudley Rd Location - Auto Maintenance Package (Three Complete Oil Changes, Two Tire Rotations, Diagnostics, and Inspections) 
               [value] => Array 
                (
                 [currencyCode] => USD 
                 [amountInBaseUnit] => 17900 
                ) 
    
               [price] => Array 
                (
                 [currencyCode] => USD 
                 [amountInBaseUnit] => 3000 
                ) 
    
              ) 
    
             [1] => Array 
              (
               [title] => 7892 Sudley Rd Location - Auto Maintenance Package (Three Complete Oil Changes, Two Tire Rotations, Diagnostics, and Inspections) 
               [value] => Array 
                (
                 [currencyCode] => USD 
                 [amountInBaseUnit] => 17900 
                ) 
    
               [price] => Array 
                (
                 [currencyCode] => USD 
                 [amountInBaseUnit] => 3000 
                ) 
    
              )       
    
            ) 
    
           [geographies] => Array 
            (
             [0] => Array 
              (
               [seoName] => northern-virginia 
               [displayName] => Northern Virginia 
              ) 
    
             [1] => Array 
              (
               [seoName] => montgomery-county 
               [displayName] => Montgomery County 
              ) 
    
             [2] => Array 
              (
               [seoName] => washington-dc 
               [displayName] => Washington, D.C. 
              ) 
    
             [3] => Array 
              (
               [seoName] => arlington-alexandria 
               [displayName] => Arlington/Alexandria 
              ) 
    
            ) 
    
           [redemptionLocations] => Array 
            (
             [0] => Array 
              (
               [addressPostalCode] => 20109 
               [addressStateOrProvince] => VA 
               [addressStreet1] => Battlefield BP 
               [addressStreet2] => 7111 Sudley Rd 
               [geography] => Array 
                (
                 [displayName] => Northern Virginia 
                ) 
    
               [latitude] => 38.799067 
               [longitude] => -77.518125 
               [phoneNumber] => 480-921-8282 
              ) 
            ) 
    
          ) 
    
         [1] => Array 
          (
           [category] => Array 
            (
             [name] => Indian 
             [path] => Array 
              (
               [0] => Restaurants 
               [1] => Indian 
              ) 
    
            ) 
    
           [websiteTitle] => $15 to Spend on Food and Drink 
           [description] => <p>Dine on savory Pakistani and Indian cuisine at this eatery, where everything is Halal. Enjoy kabobs, curries, and Biryani&#8212but don't miss out on their house specialty, The Karahi, with chicken, beef, goat, lamb, fish or paneer:</p> 
    
    • $ 7 $ 15至花在食品和非酒精飲料
    • 多樣化的菜單包括包裝,沙拉,咖喱,和燒烤最愛
    • 我們建議木炭雞組合,其中包括溫暖烤餅和美味的雙方

    Charcoal Chicken的網站| Facebook的

       [finePrint] => <ul><li>Limit 2 per customer </li> 
    

  • 限制1每桌每次訪問
  • 僅於堂食或外賣有效
  • 排除酒精
  • 不包括節假日
  • 整個價值必須在一個訪問中使用
  • 購買後可開始使用
  • 2014年10月12日之後的促銷價到期
  • PAID值到期5年從購買日起
  • [ASIN] => B00L1OHHZK [IMAGEURL] => /圖像/ G/01 /餘燼/交易/ ab799f011c6041f1d99b776d4e11f8b7e0e3c2422a6fd1ccae595d5c70c2937c [商家] =>數組 ( [顯示名] =>炭火烤雞 )

       [offerEndTime] => 1405148400000 
           [options] => Array 
            (
             [0] => Array 
              (
               [title] => $15 to Spend on Food and Nonalcoholic Drinks 
               [value] => Array 
                (
                 [currencyCode] => USD 
                 [amountInBaseUnit] => 1500 
                ) 
    
               [price] => Array 
                (
                 [currencyCode] => USD 
                 [amountInBaseUnit] => 700 
                ) 
    
              ) 
    
            ) 
    
           [geographies] => Array 
            (
             [0] => Array 
              (
               [seoName] => northern-virginia 
               [displayName] => Northern Virginia 
              ) 
    
            ) 
    
           [redemptionLocations] => Array 
            (
             [0] => Array 
              (
               [addressPostalCode] => 20151 
               [addressStateOrProvince] => VA 
               [addressStreet1] => 13969 Metrotech Drive 
               [addressStreet2] => 
               [geography] => Array 
                (
                 [displayName] => Northern Virginia 
                ) 
    
               [latitude] => 38.895558 
               [longitude] => -77.4283257 
               [phoneNumber] => 703-953-3700 
              ) 
    
            ) 
    
          ) 
    
         [2] => Array 
          (
           [category] => Array 
            (
             [name] => Watches 
             [path] => Array 
              (
               [0] => Retail Products 
               [1] => Watches 
              ) 
    
            ) 
    
           [websiteTitle] => Watch-Battery Replacement or Watch Repair 
           [description] => <p>Fashion Time is the premier spot in Maryland and Virginia to buy and repair all your timepieces. Whether you're in the market for a grandfather clock or a new stem for your watch, these time experts can help. </p><ul><li>$6 ($13 value) for a battery replacement for a non-Swiss watch</li><li>$19 ($40 value) for a battery replacement for a Swiss watch</li><li>$20 for $40 to spend on watches or watch repair</li><li>Old-fashioned craftsmen and technicians are experts in timepieces </li></ul><p>Fashion Time on <a href="https://www.facebook.com/pages/Fashion-Time/150334105035439" target="_blank">Facebook</a></p> 
           [finePrint] => <p></p><ul><li>Buy as many as you like; send as many as you like as gifts<br></li><li>Limit 1 voucher per customer per visit<br></li><li>Valid only for option purchased<br></li><li>Excludes sale items<br></li><li>Valid only for in-store purchases<br></li><li>Cannot be combined with any other offers or promotions<br></li><li>Entire value per voucher must be used in a single visit </li><li>Available for use beginning the day after purchase</li><li>PROMOTIONAL VALUE EXPIRES 180 DAYS FROM THE PURCHASE DATE</li><li>PAID VALUE EXPIRES 5 YEARS FROM THE PURCHASE DATE</li></ul><p></p> 
           [asin] => B00KO8KOMM 
           [imageURL] => /images/G/01/ember/deals/7d648552caa12945ae14ee4554d255b21e1ae7739fce1f4be07ebf83e495e4 
           [merchant] => Array 
            (
             [displayName] => Fashion Time 
            ) 
    
           [offerEndTime] => 1411542000000 
           [options] => Array 
            (
             [0] => Array 
              (
               [title] => Battery Replacement for a Non-Swiss Watch 
               [value] => Array 
                (
                 [currencyCode] => USD 
                 [amountInBaseUnit] => 1300 
                ) 
    
               [price] => Array 
                (
                 [currencyCode] => USD 
                 [amountInBaseUnit] => 600 
                ) 
    
              ) 
    
             [1] => Array 
              (
               [title] => Battery Replacement for a Swiss Watch 
               [value] => Array 
                (
                 [currencyCode] => USD 
                 [amountInBaseUnit] => 4000 
                ) 
    
               [price] => Array 
                (
                 [currencyCode] => USD 
                 [amountInBaseUnit] => 1900 
                ) 
    
              ) 
    
            ) 
    
           [geographies] => Array 
            (
             [0] => Array 
              (
               [seoName] => northern-virginia 
               [displayName] => Northern Virginia 
              ) 
    
             [1] => Array 
              (
               [seoName] => montgomery-county 
               [displayName] => Montgomery County 
              ) 
    
            ) 
    
           [redemptionLocations] => Array 
            (
             [0] => Array 
              (
               [addressPostalCode] => 20166 
               [addressStateOrProvince] => VA 
               [addressStreet1] => 2110 D Dulles Town Ctr 
               [addressStreet2] => 
               [geography] => Array 
                (
                 [displayName] => Northern Virginia 
                ) 
    
               [latitude] => 39.035249 
               [longitude] => -77.42987 
               [phoneNumber] => 571-434-8875 
              ) 
    
             [1] => Array 
              (
               [addressPostalCode] => 22102 
               [addressStateOrProvince] => VA 
               [addressStreet1] => 1961 Chain Bridge Rd 
               [addressStreet2] => 
               [geography] => Array 
                (
                 [displayName] => Northern Virginia 
                ) 
    
               [latitude] => 38.91971111 
               [longitude] => -77.2259265 
               [phoneNumber] => 703-893-9005 
              ) 
    
            ) 
    
          )    
    
        ) 
    

    回答

    2

    你告訴json_decode()給你一個數組,但隨後嘗試訪問其值像一個對象。要麼返回一個對象...

    $response = json_decode(file_get_contents($file)); 
    

    ...或訪問使用數組語法這些值:

    foreach ($response as $mydeal) 
    { 
        $category  = $mydeal['category']['name']; 
        $title   = $mydeal['websiteTitle']; 
        $finePrint  = $mydeal['finePrint']; 
        $imageURL  = $mydeal['imageURL']; 
        $merchant  = $mydeal->merchant['displayName']; 
        $streetaddress1 = $mydeal['redemptionLocations']['addressStreet1']; 
    } 
    
    +0

    我已經根據您的意見修改了我的代碼。這是我的PHP代碼,它仍然不起作用。 <?php \t $ file =「http://localsurch.com/deals2。TXT「; \t $響應= json_decode(的file_get_contents($文件)); \t的foreach($響應爲$ mydeal) \t { \t \t $類別= $ mydeal [ '類別'] [ '名稱'] ; \t \t $標題= $ mydeal [ 'websiteTitle']; \t \t $的FinePrint = $ mydeal [ '的FinePrint']; \t \t $ IMAGEURL = $ mydeal [ 'IMAGEURL']; \t \t $商家= $ mydeal-> merchant ['displayName']; \t \t $ streetaddress1 = $ mydeal ['redemptionLocations'] ['addressStreet1']; \t} \t?> – user3786547

    相關問題