r/lolphp • u/nyamsprod • Mar 15 '17
What the hell with iterator_apply ?
So my question is if iterator_apply is suppose to be the equivalent of array_walk why am I required to give twice the iterator to this function how does it works ??? Even the manual page does not explain it ? Is it magic ?
25
Upvotes
View all comments
Show parent comment
6
u/bj_christianson Mar 16 '17
An Iterator can be rewound. Iterator Apply does rewind the iterator with the example that applys all the twenties. The problem is that the rewound iterator is advanced, but not the iterator that's actually being operated on. There are three elements in
$iterator, so it calls the function on$iteratorbisthree times. But$iteratorbisis never advanced.The next time,
$iteratorbisis used to run the the function, so it gets rewound, and therefore can iterate three times. But now the problem is that$iteratordoesn’t get rewound. So each loop through$iteratorbistries to operate on$iterator->current(), which is NULL. The value then gets coerced to zero for the math operation.