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>\
";
}
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.
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.
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.
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.