Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialFurquan Ahmad
5,148 PointsHow do i return certain conditions from this query
How do i return just the EAN barcode and the name from the product using php for loop ?
<?php
stdClass Object (
[StatusCode] => 0 [StatusInfo] => Command Processed OK [PageNumber] => 1 [TotalPageCount] => 2 [TotalProductCount] => 36 [PageProductCount] => 20
[Products] => Array (
[0] => stdClass Object ( [BaseProductId] => 76236849 [EANBarcode] => 5449000958938 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/938/5449000958938/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Coca Cola Diet Coke 1.75L [OfferPromotion] => Any 2 for £2.00 Offer [OfferValidity] => valid for delivery from 29 July 2015 until 19 Aug 2015 [OfferLabelImagePath] => http://www.tesco.com/Groceries/UIAssets/I/Sites/Retail/Superstore/Online/Product/pos/2for.png [ShelfCategory] => [ShelfCategoryName] => [Price] => 1.85 [PriceDescription] => £1.85 each [ProductId] => 282822379 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.106 [UnitType] => 100ml )
[1] => stdClass Object ( [BaseProductId] => 54109896 [EANBarcode] => 5449000123855 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/855/5449000123855/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Diet Coke 24 X 330Ml Pack [OfferPromotion] => [OfferValidity] => [OfferLabelImagePath] => [ShelfCategory] => [ShelfCategoryName] => [Price] => 7 [PriceDescription] => £7.00 each [ProductId] => 273824391 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.088 [UnitType] => 100ml )
[2] => stdClass Object ( [BaseProductId] => 76236757 [EANBarcode] => 5449000130389 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/389/5449000130389/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Coca-Cola Coke 1.75L [OfferPromotion] => Any 2 for £2.00 Offer [OfferValidity] => valid for delivery from 29 July 2015 until 19 Aug 2015 [OfferLabelImagePath] => http://www.tesco.com/Groceries/UIAssets/I/Sites/Retail/Superstore/Online/Product/pos/2for.png [ShelfCategory] => [ShelfCategoryName] => [Price] => 1.85 [PriceDescription] => £1.85 each [ProductId] => 282822189 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.106 [UnitType] => 100ml )
[3] => stdClass Object ( [BaseProductId] => 50703262 [EANBarcode] => 5449000052957 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/957/5449000052957/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Diet Coke Gb 12 X 330Ml [OfferPromotion] => [OfferValidity] => [OfferLabelImagePath] => [ShelfCategory] => [ShelfCategoryName] => [Price] => 3.55 [PriceDescription] => £3.55 each [ProductId] => 260346509 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.108 [UnitType] => 100ml ) [4] => stdClass Object ( [BaseProductId] => 76236901 [EANBarcode] => 5449000080806 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/806/5449000080806/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Coca-Cola Coke Zero 1.75L [OfferPromotion] => Any 2 for £2.00 Offer [OfferValidity] => valid for delivery from 29 July 2015 until 19 Aug 2015 [OfferLabelImagePath] => http://www.tesco.com/Groceries/UIAssets/I/Sites/Retail/Superstore/Online/Product/pos/2for.png [ShelfCategory] => [ShelfCategoryName] => [Price] => 1.85 [PriceDescription] => £1.85 each [ProductId] => 282822492 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.106 [UnitType] => 100ml ) [5] => stdClass Object ( [BaseProductId] => 75791935 [EANBarcode] => 5449000136909 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/909/5449000136909/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Coca Cola Coke Zero 24X330ml [OfferPromotion] => [OfferValidity] => [OfferLabelImagePath] => [ShelfCategory] => [ShelfCategoryName] => [Price] => 7 [PriceDescription] => £7.00 each [ProductId] => 281926402 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.088 [UnitType] => 100ml ) [6] => stdClass Object ( [BaseProductId] => 76237278 [EANBarcode] => 5449000197313 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/313/5449000197313/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Coca Cola Diet Coke Caffeine Free 1.75L [OfferPromotion] => Any 2 for £2.00 Offer [OfferValidity] => valid for delivery from 29 July 2015 until 19 Aug 2015 [OfferLabelImagePath] => http://www.tesco.com/Groceries/UIAssets/I/Sites/Retail/Superstore/Online/Product/pos/2for.png [ShelfCategory] => [ShelfCategoryName] => [Price] => 1.85 [PriceDescription] => £1.85 each [ProductId] => 282843066 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.106 [UnitType] => 100ml ) [7] => stdClass Object ( [BaseProductId] => 66101840 [EANBarcode] => 5449000154774 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/774/5449000154774/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Diet Coke Caffeine Free 8X330ml [OfferPromotion] => [OfferValidity] => [OfferLabelImagePath] => [ShelfCategory] => [ShelfCategoryName] => [Price] => 3 [PriceDescription] => £3.00 each [ProductId] => 266799522 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.114 [UnitType] => 100ml ) [8] => stdClass Object ( [BaseProductId] => 66101068 [EANBarcode] => 5449000031341 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/341/5449000031341/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Coke Zero 8X330ml [OfferPromotion] => [OfferValidity] => [OfferLabelImagePath] => [ShelfCategory] => [ShelfCategoryName] => [Price] => 3 [PriceDescription] => £3.00 each [ProductId] => 266798726 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.114 [UnitType] => 100ml ) [9] => stdClass Object ( [BaseProductId] => 53658788 [EANBarcode] => 5449000057334 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/334/5449000057334/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Diet Coke Caffeine Free 12 X 330Ml [OfferPromotion] => [OfferValidity] => [OfferLabelImagePath] => [ShelfCategory] => [ShelfCategoryName] => [Price] => 3.55 [PriceDescription] => £3.55 each [ProductId] => 257178418 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.09 [UnitType] => 100ml ) [10] => stdClass Object ( [BaseProductId] => 76236786 [EANBarcode] => 5449000146069 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/069/5449000146069/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Coca-Cola Diet Coke 4X1.5L [OfferPromotion] => [OfferValidity] => [OfferLabelImagePath] => [ShelfCategory] => [ShelfCategoryName] => [Price] => 3.72 [PriceDescription] => £3.72 each [ProductId] => 282822258 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.062 [UnitType] => 100ml ) [11] => stdClass Object ( [BaseProductId] => 60962031 [EANBarcode] => 5449000162717 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/717/5449000162717/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Cherry Coke 8X330ml [OfferPromotion] => [OfferValidity] => [OfferLabelImagePath] => [ShelfCategory] => [ShelfCategoryName] => [Price] => 3 [PriceDescription] => £3.00 each [ProductId] => 262207672 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.114 [UnitType] => 100ml ) [12] => stdClass Object ( [BaseProductId] => 76236803 [EANBarcode] => 5449000088260 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/260/5449000088260/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Coca-Cola Coke 4X1.5L [OfferPromotion] => [OfferValidity] => [OfferLabelImagePath] => [ShelfCategory] => [ShelfCategoryName] => [Price] => 3.72 [PriceDescription] => £3.72 each [ProductId] => 282822304 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.062 [UnitType] => 100ml ) [13] => stdClass Object ( [BaseProductId] => 55885734 [EANBarcode] => 5449000064141 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/141/5449000064141/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Diet Coke 1.25 Ltrs Bottle [OfferPromotion] => [OfferValidity] => [OfferLabelImagePath] => [ShelfCategory] => [ShelfCategoryName] => [Price] => 1.55 [PriceDescription] => £1.55 each [ProductId] => 252047858 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.124 [UnitType] => 100ml ) [14] => stdClass Object ( [BaseProductId] => 76236826 [EANBarcode] => 5449000197092 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/092/5449000197092/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Coca-Cola Coke Cherry 1.75L [OfferPromotion] => Any 2 for £2.00 Offer [OfferValidity] => valid for delivery from 29 July 2015 until 19 Aug 2015 [OfferLabelImagePath] => http://www.tesco.com/Groceries/UIAssets/I/Sites/Retail/Superstore/Online/Product/pos/2for.png [ShelfCategory] => [ShelfCategoryName] => [Price] => 1.85 [PriceDescription] => £1.85 each [ProductId] => 282822342 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.106 [UnitType] => 100ml ) [15] => stdClass Object ( [BaseProductId] => 66101984 [EANBarcode] => 5449000154743 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/743/5449000154743/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Diet Coke Cherry 8 X 330Ml [OfferPromotion] => [OfferValidity] => [OfferLabelImagePath] => [ShelfCategory] => [ShelfCategoryName] => [Price] => 3 [PriceDescription] => £3.00 each [ProductId] => 266799666 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.114 [UnitType] => 100ml ) [16] => stdClass Object ( [BaseProductId] => 75274897 [EANBarcode] => 5449000195524 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/524/5449000195524/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Coca Cola Coke Zero Cherry 8X330 Ml [OfferPromotion] => [OfferValidity] => [OfferLabelImagePath] => [ShelfCategory] => [ShelfCategoryName] => [Price] => 3 [PriceDescription] => £3.00 each [ProductId] => 280878304 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.114 [UnitType] => 100ml ) [17] => stdClass Object ( [BaseProductId] => 73632874 [EANBarcode] => 5449000196620 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/620/5449000196620/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Coca Cola Vanilla Coke 8X330ml [OfferPromotion] => [OfferValidity] => [OfferLabelImagePath] => [ShelfCategory] => [ShelfCategoryName] => [Price] => 3 [PriceDescription] => £3.00 each [ProductId] => 277392905 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.114 [UnitType] => 100ml ) [18] => stdClass Object ( [BaseProductId] => 74535896 [EANBarcode] => 5449000154897 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/897/5449000154897/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Diet Coke Citrus Zest 8X330 Ml [OfferPromotion] => [OfferValidity] => [OfferLabelImagePath] => [ShelfCategory] => [ShelfCategoryName] => [Price] => 3 [PriceDescription] => £3.00 each [ProductId] => 279377351 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.114 [UnitType] => 100ml ) [19] => stdClass Object ( [BaseProductId] => 54167273 [EANBarcode] => 5449000039637 [CheaperAlternativeProductId] => [HealthierAlternativeProductId] => [ImagePath] => http://img.tesco.com/Groceries/pi/637/5449000039637/IDShot_90x90.jpg [MaximumPurchaseQuantity] => 99 [Name] => Diet Coke Contour 6 X 330Ml Bottle [OfferPromotion] => Buy 1 SIDE 1 MAIN 1 DESSERT ... Offer [OfferValidity] => valid for delivery from 1 June 2015 until 23 Aug 2015 [OfferLabelImagePath] => http://www.tesco.com/Groceries/UIAssets/I/Sites/Retail/Superstore/Online/Product/pos/save.png [ShelfCategory] => [ShelfCategoryName] => [Price] => 4.49 [PriceDescription] => £4.49 each [ProductId] => 252203236 [ProductType] => QuantityOnlyProduct [UnitPrice] => 0.227 [UnitType] => 100ml ) ) )
MOD: edited to format beginning of code.
2 Answers
shadryck
12,151 PointsYou can acces the key EANBarcode in the array with Object['EANBarcode'].
The same for the key name, Object['name'].
I'm not sure what you meant with using a for loop since there are many things you can do with an array and a for loop. Can you try to elaborate your question a bit more specific?
Ted Sumner
Courses Plus Student 17,967 PointsI had not heard of stdClass before your post so this is just a guess as what you have to do. You have a class named Object that contains an array. The contents of the array are also a stdClass named Object. I am not sure this is valid and would certainly say that it is not good practice because it is confusing.
What I have seen about stdClass shows that it is invoked without a formal class definition like this:
<?php
$stuff = new stdClass;
$arrayOfProductInfo = array (your array);
$data; //create variable for foreach loop
//assign values like this
$stuff->products = $arrayOfProductInfo;
//then I think you could use a foreach loop to pull info from the object. I have not done this but I think it would work.
foreach ($stuff->products as $product) {
$data .= $product["EANBarCode"] . " " . $product["name"] . "<br>";
}
But your stdClass is different in that it is sort of named as a class, but does not have the correct syntax as I know it. Formal class declarations are like this:
<?php
class Object {
//class stuff here
}
My first question is whether this data is from functioning code. If so, here are some ideas for accessing the data. Again, I have not used this before, so this is just an educated guess.
<?php
$workingVar = $Object->Products;
foreach ($workingVar as $product) {
echo $product->Object["EANBarCode"] . " " . $product->Object["name"] . "<br>";
}
I think you will run into problems since you have two "Object" stdClasses defined and neither one is defined the way I have read other stdClasses have been defined. Let me know if any of these ideas work for you.
Furquan Ahmad
5,148 PointsFurquan Ahmad
5,148 Pointsi'm trying to retrieve data from this array just containing the barcode and nutritional information