|
|
 |
reset (PHP 3, PHP 4, PHP 5) reset --
Set the internal pointer of an array to its first element
Descriptionmixed reset ( array &array )
reset() rewinds array's
internal pointer to the first element and returns the value of the first
array element, or FALSE if the array is empty.
Example 1. reset() example |
<?php
$array = array('step one', 'step two', 'step three', 'step four');
echo current($array) . "<br />\n"; next($array);
next($array);
echo current($array) . "<br />\n"; reset($array);
echo current($array) . "<br />\n"; ?>
|
|
See also current(),
each(), next(),
and prev().
User Contributed Notes
reset
s_p_a_mcatcher at hotmail dot com
02-Dec-2004 12:30
Be aware that when using reset() to clear an element and key from an array, if auto-incrementing, the new array keys will not reset a key previously set:
$temparray[] = "0";
$temparray[] = "1";
$temparray[] = "2";
unset($temparray[2]);
$temparray[] = "2";
$temparray[] = "3";
print_r($temparray);
The above will return:
Array
(
[0] => 0
[1] => 1
[3] => 2
[4] => 3
)
When attempting something like this, its better to use array_pop().
hope it helped
leaetherstrip at inbox dot NOSPAMru
17-Oct-2004 08:54
Note that reset() will not affect sub-arrays of multidimensional array.
For example,
<?php
$arr = array(
1 => array(2,3,4,5,6),
2 => array(6,7,8,9,10)
);
while(list($i,) = each($arr))
{
echo "IN \$arr[$i]<br>";
while(list($sub_i,$entry) = each($arr[$i]))
{
echo "\$arr[$i][$sub_i] = $entry<br>";
}
}
reset($arr);
while(list($i,) = each($arr))
{
echo "IN \$arr[$i]<br>";
while(list($sub_i,$entry) = each($arr[$i]))
{
echo "\$arr[$i][$sub_i] = $entry<br>";
}
}
?>
will print
IN $arr[1]
$arr[1][0] = 2
$arr[1][1] = 3
$arr[1][2] = 4
$arr[1][3] = 5
$arr[1][4] = 6
IN $arr[2]
$arr[2][0] = 6
$arr[2][1] = 7
$arr[2][2] = 8
$arr[2][3] = 9
$arr[2][4] = 10
IN $arr[1]
IN $arr[2]
kevin at oceania dot net
18-Jul-2003 08:54
Here is a simple example on how to combine 2 arrays. Here we use array_combine() to create list of months and there respective month number. The same result could just as easily be achieved with array('1'=>'January') etc.
<?php
error_reporting(E_ALL);
$keys = range(1,12);
$months = array(
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
);
$combined_array = array_combine($keys, $months);
foreach($combined_array as $k=>$v){ echo $k.' -> '.$v.'<br />'; }
?>
jules
12-Jun-2003 02:29
Be aware that if you give an empty array to reset(), what you'll get back is a boolean. consider...
$myarray = array();
$ret = reset($myarray);
echo 'reset has type '. gettype($ret) .' and val * '. $ret;
if( $ret )
{
echo '*. But it evaluates true';
}
else
{
echo '*. It evaluates false';
}
if( is_null($ret) )
{
echo ', and appears null';
}
{
echo ', and appears not null';
}
echo '.';
<output>
reset has type boolean and val * *. It evaluates false, and appears not null.
</output>
<bleaugh/>
tac at smokescreen dot org
26-Sep-2001 01:42
Related to resetting an array is resetting a result set back to the beginning, so you can loop through it again. To do that, use
mysql_data_seek($result, 0)
I use this often when debugging -- do a query, print it out, then loop through it a second time to process. I'm adding this here under Reset because that's where I originally looked for this functionality.
01-Jul-2001 09:30
A cleaner (better?) way would be to use is_array() instead:
if (is_array($form_array)) {
[array stuff here]
}
kk at shonline dot de
30-Jul-1998 07:22
When used on a scalar or unset value, reset() spews warning messages. This is often a problem when accessing arrays generated from HTML form input data: these are scalar or unset if the user didn't enter sufficient information.
You can silence these error messages by prefixing an @ (at sign) to reset(), but it is better style to protect your reset() and the following array traversal with an if (isset()). Example code:
if (isset($form_array)) {
reset($form_array);
while (list($k, $v) = each($form_array) {
do_something($k, $v);
}
}
| |