What is a stdClass Object in PHP and how do I work with it?

I am playing around with cj web services and SOAP I can make the query and I get an array from the results stored in a single variable. When I

print_r $result;

I get an array that looks like:

stdClass Object ( [out] => stdClass Object ( [count] => 10 [offset] => 0 [products] => stdClass Object ( [Product] => Array ( [0] => stdClass Object ( [adId] => 10701012 [advertiserId] => 2780113 [advertiserName] => PetStore.com / MarineDepot.com [buyUrl] => http://www.dpbolvw.net/click-3943640-10701012?url=http%3A%2F%2Fwww.petstore.com%2Fps_ViewItem.aspx%3Fidproduct%3DVY68756%26source%3Dcj&cjsku=VY68756 [catalogId] => cjo:1127 [currency] => USD [description] => Tiny tails dog toy just born four on the floor giraffe [imageUrl] => http://cse.f3images.com/IMD/feeds/VY68756_50.jpg [inStock] => [isbn] => [manufacturerName] => Vo-Toys [manufacturerSku] => VY [name] => Votoy Tiny Tails Dog Toy Just Born Four On The Floor Giraffe [price] => 2.99 [retailPrice] => 2.99 [salePrice] => 2.99 [sku] => VY68756 [upc] => 075726687569 ) [1] => stdClass Object ( [adId] => 10766026 [advertiserId] => 2916010 [advertiserName] => Birthday in a Box [buyUrl] => http://www.anrdoezrs.net/click-?url=http%3A%2F%2Fwww.birthdayinabox.com%2Fparty-supplies%2Fproductdetail.asp%3Fprodsku%3D6770&cjsku=6770 [catalogId] =>

how would I take an array like this and create a foreach() statement when the array is stored in a variable called $result ?

Is this any clearer?

$response = new stdClass;
$response->products = array();


  object(stdClass)#1 (1) {
    array(0) {

foreach($response->products as $product){
  #do stuff

stdClass is PHP’s class prototype. It’s the simplest possible object - and is the eqivalent of JavaScript’s Object.prototype. All classes implicitly extend from it unless they extend from something else.

I don’t personally use it - it’s a poor man’s array at best. You can iterate over it, but you lose the power of the various array related functions whenever you use it.

The only array in there is named Product and you can loop over the products like:

foreach ($results->products->Product as $product) {
	// Do something with each product
	echo $product->name . " from " . $product->advertiserName . "<br>\

Thanks for the quick responses guys.

Is there any way to convert it to a more efficient mysql or xml type array?
I actually have a few different questions regarding the data I can and can’t get using curl, dom, and setting headers and stuff, just didnt know how to go about doing it without bombarding the forum.

I also tried code similar too:

foreach ($results->out->products->description as $product) {
		            echo "Advertiser Name: ".$product->advertiserName."\
		            echo "Category: ".$product->category."\

but I get the same error “Notice: Undefined property: stdClass::$products” each time. Isn’t the variable being declared in the foreach() statement? What is “stdClass::$products” all about?

You can’t just make up properties (->[i]something[/i] is a property) randomly, you need to follow the structure. In your example, there must be an out property (of the $results object), which itself be an object with a products property, and that must be an object with a description property (which is the array you loop over with foreach).

The stdClass::$products is just the representation that PHP uses in the error message. The format is, [i]class name[/i]::$[i]property name[/i].

The undefined property error means that $results->out is an object, but does not have a products property.

Merry Christmas Everyone.

Thanks Salathe for you response. Unfortunately I am missing something. I tried playing with the code yesterday and then again this morning with no luck.

Can you or someone point me in the direction for what I would have to google to learn or understand better the arguments in the foreach() statement ?

am not following the whole “$results->products->Product as $product” very well I am gonna need to understand what I am doing before I can edit it.

Sorry if this is getting bothersome, I am sure it’s something silly am missing it always is.

Not true.

class Foo {}

$foo = new Foo();

if ($foo instanceof stdClass) {
	echo 'Yep';
} else {
	echo 'Nope';

output is Nope

I said “Implicitly extends”. You’re giving an example of a test of Explicit extension. Implicit and Explicit do not mean the same thing - in fact they are antonyms.

If the PHP engine allowed ($foo instanceof stdClass) it would amount to being a cryptic equivalent to is_object(). That isn’t very useful and it doesn’t bother me in the slightest that it’s not allowed. But yes, I’ve known about your example for some time - that’s why I said “implicitly extends” because the engine hides the relation. At the engine level though the code for stdClass is used as the starting point of every object in the system.