What is the PHP function each () for?

+6
source share
3 answers

Nothing, now foreach() exists.

+10
source

According to PHP documentation :

each () is usually used in conjunction with a list () to move the array.

 <?php $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry'); reset($fruit); while (list($key, $val) = each($fruit)) { echo "$key => $val\n"; } ?> 
+5
source

I assume that it was PHP 3, which did not support foreach , and was retained in later versions for compatibility. For instance:

 for(;$x=each($myArray),$key=$x['key'],$value=$x['value'];) { // do something } 

It will be closest to the foreach .

+1
source

Source: https://habr.com/ru/post/888367/


All Articles