|
|
|
|
| Tutorial |
This function makes PHP’s internal pointer move back to the first element of the
array, whose value is also the return value of reset().
The following script accesses the same array twice, in two different loops, both
times using each():
$my_array = array(“Landon” => 1, “Graeme” => 2, “Tobias” => 3, “Till” => 4);
print(“<h2>Looping without reset()</h2>”);
print(“<h3>First loop</h3>”);
for($i = 0; $i < 2; $i++)
{
list($key, $value) = each($my_array);
print(“Key $key, Value $value<br>”);
}
print(“<h3>Second loop</h3>”);
for($i = 0; $i < 2; $i++)
{
list($key, $value) = each($my_array);
print(“Key $key, Value $value<br>”);
}
As the output in Figure 2.5 shows, the second loop will not start from the first element
again; instead, it continues where the first loop left off.This is due to PHP’s internal
array pointer not having been reset. A small modification to the script creates a
different result (see Figure 2.6):
$my_array = array(“Landon” => 1, “Graeme” => 2, “Tobias” => 3, “Till” => 4);
print(“<h2>Looping with reset()</h2>”);
print(“<h3>First loop</h3>”);
for($i = 0; $i < 2; $i++)
{
list($key, $value) = each($my_array);
print(“Key $key, Value $value<br>”);
}
print(“<h3>Calling reset()</h3>”);
reset($my_array);
print(“<h3>Second loop</h3>”);
for($i = 0; $i < 2; $i++)
{
list($key, $value) = each($my_array);
print(“Key $key, Value $value<br>”);
}
|
|
|
|
|
|
| Link Partners: Asia florist, Flowers to India, Hong kong flowers, Site submit, Cheap web hosting, China florist, Japan florist |
|