eftychia: Me in poufy shirt, kilt, and Darth Vader mask, playing a bouzouki (vader)
Add MemoryShare This Entry

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!

There are 3 comments on this entry. (Reply.)
 
posted by [identity profile] acroyear70.livejournal.com at 02:28pm on 2007-03-24
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.
 
posted by [identity profile] dglenn.livejournal.com at 07:26pm on 2007-03-24
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.
 
posted by [identity profile] dglenn.livejournal.com at 07:30pm on 2007-03-24
(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.

Links

January

SunMonTueWedThuFriSat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24
 
25
 
26
 
27
 
28
 
29
 
30
 
31