each
(PHP 4, PHP 5, PHP 7)
each — 配列から現在のキーと値のペアを返して、カーソルを進める
この関数は PHP 7.2.0 で 非推奨 になり、PHP 8.0.0 で 削除 されました。この関数に頼らないことを強く推奨します。
説明
配列から現在のキーと値のペアを返して、カーソルを進めます。
each() を実行した後、配列カーソルは、 配列の次の要素に移動します。配列の最終要素にカーソルがある場合は、 最終要素にとどまります。再度 each を使用して配列を走査するには、 reset() を使用する必要があります。
パラメータ
array
-
入力の配列。
戻り値
配列 array
から現在のキーと値のペアを返します。
このペアは 4 つの要素を持つ配列で、
それぞれの要素は 0
, 1
,
key
, value
というキーを有しています。
要素 0
とkey
の各々は配列要素のキー名称を保持しており、1
と value
の各々はそのデータを保持しています。
配列の内部ポインタが配列の最終要素以降を指す場合、
each() は false
を返します。
例
例1 each() の例
<?php
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($foo);
print_r($bar);
?>
$bar
は、ここでは以下のキー/値のペアを保持しています。
Array ( [1] => bob [value] => bob [0] => 0 [key] => 0 )
<?php
$foo = array("Robert" => "Bob", "Seppo" => "Sepi");
$bar = each($foo);
print_r($bar);
?>
$bar は次のようなキー/値のペアを持つようになります。
Array ( [1] => Bob [value] => Bob [0] => Robert [key] => Robert )
通常 each() は、配列の走査をするために list() と共に使用します。 例えばこのようになります。
例2 each() によって配列を走査する
<?php
$fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry');
reset($fruit);
while (list($key, $val) = each($fruit)) {
echo "$key => $val\n";
}
?>
上の例の出力は以下となります。
a => apple b => banana c => cranberry
配列を他の変数に代入すると、もとの配列の内部ポインタがリセットされます。 そのため、上の例のループ内で $fruit を他の変数に代入すると、無限ループを引き起こしてしまいます。
each() をオブジェクトに対して使用することもできますが、 予期せぬ結果を返すことになるかもしれません。 オブジェクトのプロパティを each() で反復処理することはおすすめしません。
User Contributed Notes 24 notes
Following the obsolescence of the each() function, here is a way to correct your source codes:
If you use each() in a while loop like this:
while (list($Key,$Value)=@each($Array)){
....
}
you have to replace with
foreach ($Array as $Key => $Value){
....
}
In the same minds.
while (list(,$Value)=@each($Array)){
....
}
will become
foreach ($Array as $Value){
....
}
Regarding speed of foreach vs while(list) =each
I wrote a benchmark script and the results are that clearly foreach is faster. MUCH faster. Even with huge arrays (especially with huge arrays). I tested with sizes 100,000. 1,000,000 and 10,000,000. To do the test with 10 million i had to set my memory limit real high, it was close to 1gb by the time it actually worked. Anyways,
<?php
function getDiff($start, $end) {
$s = explode(' ', $start);
$stot = $s[1] + $s[0];
$e = explode(' ', $end);
$etot = $e[1] + $e[0];
return $etot - $stot;
}
$lim=10000000;
$arr = array();
for ($i=0; $i<$lim; $i++) {
$arr[$i] = $i/2;
}
$start = microtime();
foreach ($arr as $key=>$val);
$end = microtime();
echo "time for foreach = " . getDiff($start, $end) . ".\n";
reset($arr);
$start = microtime();
while (list($key, $val) = each($arr));
$end = microtime();
echo "time list each = " . getDiff($start, $end) . ".\n";
?>
here are some of my results: with 1,000,000
time for foreach = 0.0244591236115.
time list each = 0.158002853394.
desktop:/media/sda5/mpwolfe/tests$ php test.php
time for foreach = 0.0245339870453.
time list each = 0.154260158539.
desktop:/media/sda5/mpwolfe/tests$ php test.php
time for foreach = 0.0269000530243.
time list each = 0.157305955887.
then with 10,000,000:
desktop:/media/sda5/mpwolfe/tests$ php test.php
time for foreach = 1.96586894989.
time list each = 14.1371650696.
desktop:/media/sda5/mpwolfe/tests$ php test.php
time for foreach = 2.02504014969.
time list each = 13.7696218491.
desktop:/media/sda5/mpwolfe/tests$ php test.php
time for foreach = 2.0246758461.
time list each = 13.8425710201.
by the way, these results are with php 5.2 i believe, and a linux machine with 3gb of ram and 2.8ghz dual core pentium
each was deprecated because it exposed too much of the internal implementation details, blocking language development. ("We can't do X because it would break each().")
https://wiki.php.net/rfc/deprecations_php_7_2#each
If you want an array pointer, maintain it yourself. Probably a good idea anyway, because then it's visible in the code.
This function is marked as deprecated in PHP 7.2.0 and is removed in PHP 8.0.0, the replacement for legacy code would look like this:
function legacy_each($array){
$key = key($array);
$value = current($array);
$each = is_null($key) ? false : [
1 => $value,
'value' => $value,
0 => $key,
'key' => $key,
];
next($array);
return $each;
}
Hello, since each() and list() often "betray" very old applications, I simply recommend not to use them anymore.
If you want to assign variables based on an associative array,
Replace this:
while(list ($key, $value) = each ($my_array)) {
$$key = $value;
}
with this:
foreach ($my_array as $key => $value) {
$$key = $value;
}
Use foreach instead of while, list and each. Foreach is:
- easier to read
- faster
- not influenced by the array pointer, so it does not need reset().
It works like this:
<?php
$arr = array('foo', 'bar');
foreach ($arr as $value) {
echo "The value is $value.";
}
$arr = array('key' => 'value', 'foo' => 'bar');
foreach ($arr as $key => $value) {
echo "Key: $key, value: $value";
}
?>
Rector has an automated fix ('ListEachRector') to migrate away from `each()`:
https://github.com/rectorphp/rector/blob/master/docs/AllRectorsOverview.md#listeachrector
If you look at the code example you'll see this is even quite simple to do by hand.
It's worth noting that references to an array don't have thier own array pointer, and taking a reference to an array doesn't reset it's array pointer, so this works as you would expect it would by eaching the first three items of the array, rather than the first item 3 times.
<?php
$x = array(1,2,3);
print_r(each($x));
echo "\n";
$y =& $x;
print_r(each($y));
echo "\n";
$z =& $y;
print_r(each($z));
echo "\n";
?>
If you want to display the hole structure (tree) of your array, then you can use this recursive solution.
<?PHP
$tree= "";
array_tree($your_array);
echo $tree;
// Recursive Function
function array_tree($array, $index=0){
global $tree;
$space="";
for ($i=0;$i<$index;$i++){
$space .= " ";
}
if(gettype($array)=="array"){
$index++;
while (list ($x, $tmp) = each ($array)){
$tree .= $space."$x => $tmp\n";
array_tree($tmp, $index);
}
}
}
?>
An odd function to deprecate. If you're keeping track of an array pointer in a collection outside a foreach loop you don't care about performance and the utility of this function is core.
Instead you must call two functions: current() and then next() to replicate its behaviour.
If you want to iterate over a two-dimensional, sparse array, and want to first display every first element, then every second and so on, you can use this code:
$fruits = array ( "fruits" => array ( "a" => "orange",
"b" => "banana",
"c" => "apple"
),
"numbers" => array ( 1,
2,
3,
4,
5,
6
),
"holes" => array ( "first",
5 => "second",
"third",
10 => "fourth",
)
);
$done = False;
while ($done == False) {
$done = True;
// Important: &$val has to be a reference (use the &),
// if you don't, the internal counter of $val will be
// re-initialized each time and you loop over the first elements
// for eternity.
foreach($fruits as $key => &$val) {
if (list($inner_key, $inner_val) = each(&$val)) {
$done = False;
echo "$key : : $inner_key => $inner_val <br> \n";
}
}
}
NOTE: this is just a quick hack, if you know a better way, post it!
This function will help you dump any variable into XML structure.
//dump var into simple XML structure
function var_dump_xml($tagname,$variable,$level=0)
{
for($i=0;$i<$level;$i++) $marg.=' ';
if (eregi('^[0-9].*$',$tagname)) $tagname='tag_'.$tagname; //XML tag cannot start with [0-9] character
if (is_array($variable))
{
echo $marg."<$tagname>\n";
while (list ($key, $val) = each ($variable)) var_dump_xml($key,$val,$level+1);
echo $marg."</$tagname>\n";
}
elseif (strlen($variable)>0)
{
echo $marg."<$tagname>".htmlspecialchars($variable)."</$tagname>\n";
};
};
/*
example:
$myVar = array("name"=>"Joe", "age"=>"26", "children"=>array("Ann","Michael"));
var_dump_xml("myVarTag",$myVar);
*/
To panania at 3ringwebs dot com:
If you know for certain that you are only receiving one row, the while becomes redundant. To shorten your code:
$strSQL = "SELECT * FROM table WHERE id=1";
$RecordsetSelect = $db->runQuery ($strSQL);
list($key, $val) = mysql_fetch_row($RecordsetSelect);
echo "$key => $val\n";
mysql_free_result($RecordsetSelect);
With only one row being returned this is more elegant a solution, but just being nit-picky in essence. It also shows another quick way of using list.
If you forget to reset the array before each(), the same code may give different results with different php versions.
<?php
$a = array(1,2,3);
foreach ($a AS $k => $v) $a[$k] = 2*$v;
while(list($k2, $v2) = each($a)) { echo($v2."\n"); }
?>
In PHP 5.2.0:
2
4
6
In PHP 5.2.6:
4
6
I usually work a lot with 2D arrays. Since I've had some trouble traversing them correctly maybe someone out there also experienced those problems and can use this one.
It's based on a 2D-array called $array[$x][$y]. At some (but not necessarily all) (x,y) there is a value I want to reach. Note that I do not know beforehand the ranges of $x or $y (that is their highest and lowest values).
while (list ($x, $tmp) = each ($array)) {
while (list ($y, $val) = each ($tmp)) {
echo "$x, $y, $val";
}
}
The answer for each (x,y) pair can thus be (providng, of course those values where in your array beforehand):
1, 1, 2
2, 2, 0
3, 1, 1
5, 2, 2
5, 1, 2
Note that only the (x,y) pairs with a corresponding value is shown.
Hang in there
Jon Egil Strand
NTNU