Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: reset - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

rsort" width="11" height="7"/> <range
Last updated: Thu, 31 May 2007

view this page in

reset

(PHP 4, PHP 5)

reset — 配列の内部ポインタを先頭の要素にセットする

説明

mixed reset ( array &$array )

reset() は、array の内部ポインタの先頭の要素に戻し、配列の最初の要素の値か、 配列が空の場合 FALSE を返します。

例 316. reset() の例

<?php

$array
= array('step one', 'step two', 'step three', 'step four');
 
// デフォルトでは、ポインタは先頭要素を指しています
echo current($array) . "<br />\n"; // "step one"

// 次の2ステップをとばします
next($array);
next($array);
echo
current($array) . "<br />\n"; // "step three"
 
// ポインタをリセットし、再度ステップ1開始します
reset($array);
echo
current($array) . "<br />\n"; // "step one"

?>

current()each()end()next() および prev() も参照ください。



rsort" width="11" height="7"/> <range
Last updated: Thu, 31 May 2007
 
add a note add a note User Contributed Notes
reset
steffen schomberg
17-Jul-2007 09:32
The following method resets every array contained in a multi-dimensional array recursively. It takes the multi-dimensional array as parameter.

function Array_Dimensional_Reset(&$arrRef) {
    foreach ($arrRef as $key=>$val) {
        if (is_array($val)) {
            $this->Array_Dimensional_Reset($val);
            reset($arrRef[$key]);
        }
    }
}
m dot lebkowski+php at gmail dot com
03-Aug-2006 05:58
Colin, there`s a better (IMO) way to solve your problem.
<? 
 
// ...
 
foreach($a as $k => &$d){}   // notice the "&"
  // ...
?>
It`s a new feature in PHP5 to use references in foreach loop. This way PHP isn`t making a copy of the array, so the internal pointer won`t be reset.
Colin
07-Jun-2006 12:11
I had a problem with PHP 5.0.5 somehow resetting a sub-array of an array with no apparent reason.  The problem was in doing a foreach() on the parent array PHP was making a copy of the subarrays and in doing so it was resetting the internal pointers of the original array.

The following code demonstrates the resetting of a subarray:

<?
$a
= array(
   
'a' => array(
       
'A', 'B', 'C', 'D',
    ),
   
'b' => array(
       
'AA', 'BB', 'CC', 'DD',
    ),
);

// Set the pointer of $a to 'b' and the pointer of 'b' to 'CC'
reset($a);
next($a);
next($a['b']);
next($a['b']);
next($a['b']);

var_dump(key($a['b']));
foreach(
$a as $k => $d)
{
}
var_dump(key($a['b']));
?>

The result of the two var dumps are 3 and 0, respectively.  Clearly the internal pointer of $a['b'] was reset by doing the foreach loop over $a.

Each time the foreach loop iterated over the 'a' and 'b' keys of $a it made a copy of $a['a'] and $a['b'] into $d which resetted the internal pointers of $a['a'] and $a['b'] despite making no obvious changes.

The solution is instead to iterate over the keys of $a.

<?
foreach(array_keys($a) as $k)
{
}
?>

and using $a[$k] (or creating an alias of $a[$k] as $d and dealing with the consequences of using aliases).

For the curious, I was implementing the Iterator interface on a dummy object and calling a global object to do the actual iteration (also to cope with PHP's lack of C-style pointers which when doing a $a = $b on objects would cause the data in $a to be inconsistent with the data in $b when modified).  Being that I had many dummy objects representing different data sets I chose to store each data set as a subarray contained within the global object.  To make this work each dummy object has to store a key (which can freely be duplicated without problems) that it passes to the global object when rewind, key, current, next, and valid were called on the dummy object.

Unfortunately for me, my key required to be more than just a simple string or number (if it was then it could be used to directly index the subarray of data for that object and problem avoided) but was an array of strings.  Instead, I had to iterate over (with a foreach loop) each subarray and compare the key to a variable stored within the subarray.

So by using a foreach loop in this manner and with PHP resetting the pointer of subarrays it ended up causing an infinite loop.

Really, this could be solved by PHP maintaining internal pointers on arrays even after copying.
27-Feb-2006 06:20
I wrote a nice function, which rotates values of array. Very useful for table rows where you have to rotate colors

<?php
function rotate(&$array) {
   
$item = current($array);
    if (!
next($array)) reset($array);  
    return
$item;
}
?>
Alexandre Koriakine
19-Nov-2005 07:09
Also it's good to reset this way the multidimentional arrays:

reset($voo2['moder']);
while (list($key, $value) = each ($voo2['moder'])) {

reset($voo2['moder'][$key]);
while (list($key1, $value1) = each ($voo2['moder'][$key])) {
#do what u want
}

}
s_p_a_mcatcher at hotmail dot com
02-Dec-2004 03: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 10: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);

   
// Do the same again
   
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
19-Jul-2003 10: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
                                                                                                             
 
// make it or break it
 
error_reporting(E_ALL);
                                                                                                             
 
// create and array of keys
 
$keys = range(1,12);
                                                                                                             
 
// create an array of months
 
$months = array(
 
'January',
 
'February',
 
'March',
 
'April',
 
'May',
 
'June',
 
'July',
 
'August',
 
'September',
 
'October',
 
'November',
 
'December'
);
                                                                                                             
 
// combine the arrays
 
$combined_array = array_combine($keys, $months);
                                                                                                             
 
// echo out the results
 
foreach($combined_array as $k=>$v){ echo $k.' -> '.$v.'<br />'; }
                                                                                                             
?>
jules
12-Jun-2003 04: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
27-Sep-2001 03: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.
02-Jul-2001 11: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 09: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);
  }
}

rsort" width="11" height="7"/> <range
Last updated: Thu, 31 May 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites