JSON reply converted to Array shows no output when printed in PHP
JSON reply converted to Array shows no output when printed in 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] => ) ) )
Answer by T.J. Crowder for JSON reply converted to Array shows no output when printed in PHP
Look closely at the JSON, and you'll see that productInfoList
is an object with a property called productBaseInfo
whose value is an array of objects (e.g., [....]
, although it only has one entry), each of which has properties like productBaseInfo
.
So to see the first (only) entry's productBaseInfo.productIdentifier.productId
, you'd want:
$test2 = $array['productInfoList'][0]['productBaseInfo']['productIdentifier']['productId']; // Note --------------------------^^^
Answer by Kroonal for JSON reply converted to Array shows no output when printed in PHP
Use below code. It gives you title and ProductID $array = json_decode($string, true);
$test1 = $array['productInfoList'][0]['productBaseInfo']['productAttributes']['title']; $test2 = $array['productInfoList'][0]['productBaseInfo']['productIdentifier']['productId'];
echo $test1; echo $test2;
Answer by Ghulam Ali for JSON reply converted to Array shows no output when printed in PHP
In JSON { denotes and Object and [ denotes an Array. To get all the Attributes you can use something like this:
$array = json_decode($response, true); $productInfoList_Array = $array['productInfoList']; foreach($productInfoList_Array as $productInfoList){ $productBaseInfo = $productInfoList['productBaseInfo']; $productAttributes = $productBaseInfo['productAttributes']; foreach($productAttributes as $Attribute_Key => $Attribute_Value){ if (!is_array($Attribute_Value)){ //Attribute Value is not an array. echo "{$Attribute_Key}: {$Attribute_Value}"; echo "
"; }else{ //Attribute Value is a sub array foreach($Attribute_Value as $Attribute_Sub_Key => $Attribute_Sub_Value){ echo "Sub Key: {$Attribute_Sub_Key}: {$Attribute_Sub_Value}"; echo "
"; } } } echo "
End of Product."; }
See that productAttributes has sub array also.
Answer by Vincent for JSON reply converted to Array shows no output when printed in PHP
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']; }
Answer by Karthik for JSON reply converted to Array shows no output when printed in PHP
Remove all single quote. Try this.
print_r($array['productInfoList'][0]['productBaseInfo']['productIdentifier']); print_r($array['productInfoList'][0]['productBaseInfo']['productAttributes']['title']); ?>
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment