dongwen7283 2016-04-04 12:36
浏览 100
已采纳

转换为Array的JSON回复在PHP中打印时不显示输出

This is the JSON reply I get for a query I make with an API for Flipkart. I wanted to print the productId and title from the API reply in search listing page I'm designing.

{
   "productInfoList":[
      {
         "productBaseInfo":{
            "productIdentifier":{
               "productId":"MOBE89M7BPKYHZGX",
               "categoryPaths":{
                  "categoryPath":[
                     [
                        {
                           "title":"Mobiles>Handsets"
                        }
                     ]
                  ]
               }
            },
            "productAttributes":{
               "title":"Sony Xperia C4 Dual",
               "productDescription":"Are you a selfie fan? If yes, then grab this exceptional Sony Xperia C4 Dual smartphone which has been designed to click the best PROselfies. Apart from an amazing front and rear camera, this Full HD smartphone offers maximum performance and speed. All You Need For PROselfies For all selfie lovers, the 5 MP front camera of this Sony device is equipped with a soft LED flash, 25 mm wide-angle lens and other features to click the best PROselfies. Exmor R sensor ensures sharp pictures with low noise. HDR balances bright background lights for clearer results. SteadyShot lets you shoot amazing selfie videos. Unique Soft LED Flash The unique soft LED flash automatically adjusts according to the lighting conditions, illuminates the scene and gives you the perfect shot. Whether there is a bright backlight or you are taking photos at night, the Superior Auto feature ensures that your selfies and group selfies are of professional quality. 25 mm Wide View Thanks to the 25 mm wide-angle lens with an 80-degree field of view, you can fit in more of your friends or more of the view in a single shot. Main Camera This Sony Xperia smartphone is equipped with a 13 MP primary shooter. Exmor RS For Mobile This image sensor technology assures sharp photos and videos with fine detailing, amazing picture clarity and rich colors, even while shooting in challenging light conditions. Superior Auto The Superior Auto feature recognises up to 52 different scenarios such as illuminated night shots and close-up macros. This intelligent feature adjusts the settings according to the scene, so you get noteworthy photos always. HDR For Photo & Video HDR mode shoots the same image multiple times under different exposures and layers them, so that you get clear and bright photos and videos. This feature is extremely useful while capturing photos or videos in strong backlight or high-contrast situations. SteadyShot Thanks to SteadyShot, every video you shoot with this Xperia smartphone is smooth, steady and perfect. Sound Photo Relive you favourite moments with sound and visuals. Capture up to 10 seconds of audio with your photos using Sound Photo app and share it via PlayMemories Online. AR Mask Replace your own face or other people's faces with a different face using the AR Mask app. You can choose from the pre-loaded faces or create your own collection. Style Portrait This app lets you select from different styles to enhance your photos or add fun effects to them. These effects can be applied in real-time or after taking the photo or video. Download More Apps Apart from the pre-installed apps, you can download more camera apps directly from the camera interface of this Xperia smartphone. Display The large 13.97 cm (5.5) Full HD display and Mobile Bravia Engine 2 make the Xperia C4 an ideal device for watching movies and playing games. The IPS technology allows excellent viewing from all angles, so you and your friends can enjoy a movie on this device while chilling out in the college cafeteria. Sound Equipped with a speaker capable of up to 97 decibels and ClearAudio+ technologies for crystal clear audio, you can pump up your home get-togethers with loud music. This Xperia device delivers rich bass, thanks to the xLoud loudness enhancement engine. Design Despite the large screen size, the Xperia C4 is sleek, lightweight and easily slides into your jeans pocket or wallet. The device is 7.9 mm thin and weighs just 147 grams. Processor Play games lag-free, run multiple apps simultaneously, surf the web seamlessly and stream HD videos without glitches. The competent 1.7 GHz Octa Core processor and 2 GB RAM offers maximum performance in everything you do on your Xperia smartphone. The phone also comes with a built-in LTE/4G modem for extremely fast internet connectivity. Battery An incredibly powerful yet efficient 2600 mAh battery powers this Xperia phone. To further extend the already long battery life, this device offers four power management modes - Battery STAMINA mode, Extended standby mode, Extended usage mode and Ultra STAMINA mode.",
               "imageUrls":{
                  "400x400":"http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-400x400-imae8dsymwzxb6u2.jpeg",
                  "200x200":"http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-200x200-imae8dsymwzxb6u2.jpeg",
                  "unknown":"http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-original-imae8dsymwzxb6u2.jpeg",
                  "800x800":"http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-800x800-imae8dsymwzxb6u2.jpeg"
               },
               "maximumRetailPrice":{
                  "amount":29490.0,
                  "currency":"INR"
               },
               "sellingPrice":{
                  "amount":19699.0,
                  "currency":"INR"
               },
               "productUrl":"http://dl.flipkart.com/dl/sony-xperia-c4-dual/p/itme9gw2xxxgd9wy?pid=MOBE89M7BPKYHZGX&affid=admintechz",
               "productBrand":"Sony",
               "inStock":true,
               "isAvailable":true,
               "codAvailable":true,
               "emiAvailable":null,
               "discountPercentage":33.0,
               "cashBack":null,
               "offers":[

               ],
               "size":null,
               "color":"Black",
               "sizeUnit":"",
               "sizeVariants":"[MOBE89M8BXKKYMBP, MOBE89M8Y4RN9NHT, MOBE89M7BPKYHZGX]",
               "colorVariants":"[MOBE89M8BXKKYMBP, MOBE89M8Y4RN9NHT]",
               "styleCode":null
            }
         },
         "productShippingBaseInfo":{
            "shippingOptions":null
         },
         "offset":null
      }
   ]
}

This is the php code I use to print the elements in the Array. But I don't seem to get any output.

$response is the reply I get from the API.

$array = json_decode($response, true);
$test1 = $array['productAttributes']['title'];
$test2 = $array['productInfoList']['productBaseInfo']['productIdentifier']['productId'];

    echo $test1;
    echo test2;

debug -

Array ( [productInfoList] => Array ( [0] => Array ( [productBaseInfo] => Array ( [productIdentifier] => Array ( [productId] => MOBE89M7BPKYHZGX [categoryPaths] => Array ( [categoryPath] => Array ( [0] => Array ( [0] => Array ( [title] => Mobiles>Handsets ) ) ) ) ) [productAttributes] => Array ( [title] => Sony Xperia C4 Dual [productDescription] => Are you a selfie fan? If yes, then grab this exceptional Sony Xperia C4 Dual smartphone which has been designed to click the best PROselfies. Apart from an amazing front and rear camera, this Full HD smartphone offers maximum performance and speed. All You Need For PROselfies For all selfie lovers, the 5 MP front camera of this Sony device is equipped with a soft LED flash, 25 mm wide-angle lens and other features to click the best PROselfies. Exmor R sensor ensures sharp pictures with low noise. HDR balances bright background lights for clearer results. SteadyShot lets you shoot amazing selfie videos. Unique Soft LED Flash The unique soft LED flash automatically adjusts according to the lighting conditions, illuminates the scene and gives you the perfect shot. Whether there is a bright backlight or you are taking photos at night, the Superior Auto feature ensures that your selfies and group selfies are of professional quality. 25 mm Wide View Thanks to the 25 mm wide-angle lens with an 80-degree field of view, you can fit in more of your friends or more of the view in a single shot. Main Camera This Sony Xperia smartphone is equipped with a 13 MP primary shooter. Exmor RS For Mobile This image sensor technology assures sharp photos and videos with fine detailing, amazing picture clarity and rich colors, even while shooting in challenging light conditions. Superior Auto The Superior Auto feature recognises up to 52 different scenarios such as illuminated night shots and close-up macros. This intelligent feature adjusts the settings according to the scene, so you get noteworthy photos always. HDR For Photo & Video HDR mode shoots the same image multiple times under different exposures and layers them, so that you get clear and bright photos and videos. This feature is extremely useful while capturing photos or videos in strong backlight or high-contrast situations. SteadyShot Thanks to SteadyShot, every video you shoot with this Xperia smartphone is smooth, steady and perfect. Sound Photo Relive you favourite moments with sound and visuals. Capture up to 10 seconds of audio with your photos using Sound Photo app and share it via PlayMemories Online. AR Mask Replace your own face or other people's faces with a different face using the AR Mask app. You can choose from the pre-loaded faces or create your own collection. Style Portrait This app lets you select from different styles to enhance your photos or add fun effects to them. These effects can be applied in real-time or after taking the photo or video. Download More Apps Apart from the pre-installed apps, you can download more camera apps directly from the camera interface of this Xperia smartphone. Display The large 13.97 cm (5.5) Full HD display and Mobile Bravia Engine 2 make the Xperia C4 an ideal device for watching movies and playing games. The IPS technology allows excellent viewing from all angles, so you and your friends can enjoy a movie on this device while chilling out in the college cafeteria. Sound Equipped with a speaker capable of up to 97 decibels and ClearAudio+ technologies for crystal clear audio, you can pump up your home get-togethers with loud music. This Xperia device delivers rich bass, thanks to the xLoud loudness enhancement engine. Design Despite the large screen size, the Xperia C4 is sleek, lightweight and easily slides into your jeans pocket or wallet. The device is 7.9 mm thin and weighs just 147 grams. Processor Play games lag-free, run multiple apps simultaneously, surf the web seamlessly and stream HD videos without glitches. The competent 1.7 GHz Octa Core processor and 2 GB RAM offers maximum performance in everything you do on your Xperia smartphone. The phone also comes with a built-in LTE/4G modem for extremely fast internet connectivity. Battery An incredibly powerful yet efficient 2600 mAh battery powers this Xperia phone. To further extend the already long battery life, this device offers four power management modes - Battery STAMINA mode, Extended standby mode, Extended usage mode and Ultra STAMINA mode. [imageUrls] => Array ( [400x400] => http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-400x400-imae8dsymwzxb6u2.jpeg [200x200] => http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-200x200-imae8dsymwzxb6u2.jpeg [unknown] => http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-original-imae8dsymwzxb6u2.jpeg [800x800] => http://img.fkcdn.com/image/mobile/z/g/x/sony-xperia-c4-dual-na-800x800-imae8dsymwzxb6u2.jpeg ) [maximumRetailPrice] => Array ( [amount] => 29490 [currency] => INR ) [sellingPrice] => Array ( [amount] => 19699 [currency] => INR ) [productUrl] => http://dl.flipkart.com/dl/sony-xperia-c4-dual/p/itme9gw2xxxgd9wy?pid=MOBE89M7BPKYHZGX&affid=admintechz [productBrand] => Sony [inStock] => 1 [isAvailable] => 1 [codAvailable] => 1 [emiAvailable] => [discountPercentage] => 33 [cashBack] => [offers] => Array ( ) [size] => [color] => Black [sizeUnit] => [sizeVariants] => [MOBE89M8BXKKYMBP, MOBE89M8Y4RN9NHT, MOBE89M7BPKYHZGX] [colorVariants] => [MOBE89M8BXKKYMBP, MOBE89M8Y4RN9NHT] [styleCode] => ) ) [productShippingBaseInfo] => Array ( [shippingOptions] => ) [offset] => ) ) )
  • 写回答

5条回答 默认 最新

  • duanjiashai9140 2016-04-04 12:51
    关注

    You should first loop through the array values(decoded from json), and then fetch the needed data accordingly, do something like this -

    $array = json_decode($r, true);
    // print_r($array); //debug
    foreach($array['productInfoList'] as $product){
       echo  $product['productBaseInfo']['productAttributes']['title'];
       echo $product['productBaseInfo']['productIdentifier']['productId'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制