Re: Foreach problem.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Richard Lynch wrote:
Mirco Blitz wrote:

HI,
I am really confused.
I have an array, that looks like this:

print_r($elementsarr) = Array ( [0] => knr [1] => subject [2] => title [3]
=> kat [4] => pages [5] => access [6] => dofile [7] => MAX_FILE_SIZE [8]
=>
pdf [9] => dolink [10] => link [11] => erstam [12] => endless [13] => from
[14] => until [15] => openbem [16] => history [17] => closedbem [18] =>
[19]
=> b [20] => br [21] => bw [22] => bay [23] => h [24] => hb [25] => hh
[26]
=> mv [27] => n [28] => nw [29] => rp [30] => s [31] => sa [32] => sh [33]
=> sn [34] => t [35] => bund )

Now i try to work with this array in a foreach.

foreach($elementsarr as $key=>$tmp);
{
echo "$key=>$tmp<br>";
}

Now the result of that is:

35=>bund

Ist not the first time i work with foreach. But it is the first time it
just
returns the last value.

Do you have an idea why?


Perhaps you need to use http://php.net/reset

You see, when you do foreach or each or any of those, there is an
"internal" setting in the array that is altered to keep track of where you
are.  Kind of like a big "You are here" arrow inside the guts of the
array.

If you've gone through the whole thing already, you have to reset the
internal point back to the beginning.

This is not true for foreach - taken from the manual:

Note: When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop.

Note: Also note that foreach operates on a copy of the specified array and not the array itself. Therefore, the array pointer is not modified as with the each() construct, and changes to the array element returned are not reflected in the original array. However, the internal pointer of the original array is advanced with the processing of the array. Assuming the foreach loop runs to completion, the array's internal pointer will be at the end of the array.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux