|
Posted by David Haynes on 03/08/06 13:11
Toby Inkster wrote:
> David Haynes wrote:
>
>> Iteration by indexing works and is, as I said, brute force.
>
> Iteration by indexing works *iff* all your indexes are numeric and
> sequential. Your for-loop won't work on non-sequential arrays like this:
>
> array(
> 0 => '123,45',
> 1 => '123,45',
> 9 => '123,45'
> );
>
> because count() will return 3, meaning that the last element of the array,
> with index 9, won't be touched. Nor will it work on:
>
> array(
> -1 => '123,45',
> 0 => '123,45',
> 1 => '123,45'
> );
>
> as your for-loop has a hard-coded start value of 0. Nor will it work on:
>
> array(
> 'a' => '123,45',
> 'b' => '123,45',
> 'c' => '123,45'
> );
>
> because the keys aren't numeric. Iván's foreach-loop will work on all of
> these three examples.
>
Tony,
Thank you for a well presented response. I guess I tend to treat
associative and numeric arrays differently (in my own head) and so would
not even have thought of using a for() on an associative array, but the
difficulties with non-zero origin and sparse data are real.
Lesson learned.
-david-
Navigation:
[Reply to this message]
|