Warning: Invalid argument supplied for foreach()

Mar 24, 2007 10:20


Okay, I cannot figure out how to make foreach() iterate over an array of objects, assigning keys to a loop variable, in PHP4. Either what I'm trying to do constitutes abuse of the foreach() control structure, or I need more sleep. (I'm hoping it's the second of those two possibilities and will make more sense by this afternoon.)

Wheee!

programming

Leave a comment

Comments 3

acroyear70 March 24 2007, 14:28:01 UTC
object property iteration is only in php5. php isn't like javascript where object properties are the same as typed array indexes (i.e., a["hi"] == a.hi is valid javascript but not valud php (ignore my lack of $)).

doesn't look like it can be done. like classic C++ you need to know what properties are in your object before you code it.

Reply

dglenn March 24 2007, 19:26:21 UTC
Actually, my main problem was a missing '$' on the innermost array selector (the array of objects is one member of a class which itself is instantiated in an array, so it's an array of objects containing arrays of objects and golly maybe there's a less tangled way to design it now that I'm more awake). But by the time I found that, I had already stuck in a call to array_keys() for the loop instead of just sticking the array-of-objects there bare, so it sounds as though I solved the "you need PHP5 for that" problem you've outlined as a side effect of my sleepy flailing before I found the stupid typo.

Reply

dglenn March 24 2007, 19:30:01 UTC
(Er ... either I solved the object-iteration problem or I misunderstood your explanation and am actually using PHP4 unawares despite the error messages suggesting that it's PHP4.

Reply


Leave a comment

Up