Как называется съемный диск. Внешний жесткий диск: что это за устройство, для чего оно нужно и как правильно выбрать. Скорость вращения дисков

(PHP 4, PHP 5, PHP 7)

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:

foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement

The first form loops over the array given by array_expression . On each iteration, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next iteration, you"ll be looking at the next element).

The second form will additionally assign the current element"s key to the $key variable on each iteration.

In PHP 5, when foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop.

As foreach relies on the internal array pointer in PHP 5, changing it within the loop may lead to unexpected behavior.

In PHP 7, foreach does not use the internal array pointer.

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference .

$arr = array(1 , 2 , 3 , 4 );
foreach ($arr as & $value ) {
$value = $value * 2 ;
}
unset($value ); // break the reference with the last element
?>

Warning

Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset() . Otherwise you will experience the following behavior:

$arr = array(1 , 2 , 3 , 4 );
foreach ($arr as & $value ) {
$value = $value * 2 ;
}
// $arr is now array(2, 4, 6, 8)

// without an unset($value), $value is still a reference to the last item: $arr

Foreach ($arr as $key => $value ) {
// $arr will be updated with each value from $arr...
echo " { $key } => { $value } " ;
print_r ($arr );
}
// ...until ultimately the second-to-last value is copied onto the last value

// output:
// 0 => 2 Array ( => 2, => 4, => 6, => 2)
// 1 => 4 Array ( => 2, => 4, => 6, => 4)
// 2 => 6 Array ( => 2, => 4, => 6, => 6)
// 3 => 6 Array ( => 2, => 4, => 6, => 6)
?>

Before PHP 5.5.0, referencing $value is only possible if the iterated array can be referenced (i.e. if it is a variable). The following code works only as of PHP 5.5.0:

foreach (array(1 , 2 , 3 , 4 ) as & $value ) {
$value = $value * 2 ;
}
?>

foreach does not support the ability to suppress error messages using "@".

Some more examples to demonstrate usage:

/* foreach example 1: value only */

$a = array(1 , 2 , 3 , 17 );

foreach ($a as $v ) {
echo "Current value of \$a: $v .\n" ;
}

/* foreach example 2: value (with its manual access notation printed for illustration) */

$a = array(1 , 2 , 3 , 17 );

$i = 0 ; /* for illustrative purposes only */

Foreach ($a as $v ) {
echo "\$a[ $i ] => $v .\n" ;
$i ++;
}

/* foreach example 3: key and value */

$a = array(
"one" => 1 ,
"two" => 2 ,
"three" => 3 ,
"seventeen" => 17
);

foreach ($a as $k => $v ) {
echo "\$a[ $k ] => $v .\n" ;
}

/* foreach example 4: multi-dimensional arrays */
$a = array();
$a [ 0 ][ 0 ] = "a" ;
$a [ 0 ][ 1 ] = "b" ;
$a [ 1 ][ 0 ] = "y" ;
$a [ 1 ][ 1 ] = "z" ;

foreach ($a as $v1 ) {
foreach ($v1 as $v2 ) {
echo " $v2 \n" ;
}
}

/* foreach example 5: dynamic arrays */

Foreach (array(1 , 2 , 3 , 4 , 5 ) as $v ) {
echo " $v \n" ;
}
?>

Unpacking nested arrays with list()

(PHP 5 >= 5.5.0, PHP 7)

PHP 5.5 added the ability to iterate over an array of arrays and unpack the nested array into loop variables by providing a list() as the value.

$array = [
[ 1 , 2 ],
[ 3 , 4 ],
];

foreach ($array as list($a , $b )) {
// $a contains the first element of the nested array,
// and $b contains the second element.
echo "A: $a ; B: $b \n" ;
}
?>

A: 1; B: 2 A: 3; B: 4

You can provide fewer elements in the list() than there are in the nested array, in which case the leftover array values will be ignored:

A notice will be generated if there aren"t enough array elements to fill the list() :

$array = [
[ 1 , 2 ],
[ 3 , 4 ],
];

foreach ($array as list($a , $b , $c )) {
echo "A: $a ; B: $b ; C: $c \n" ;
}
?>

The above example will output:

Notice: Undefined offset: 2 in example.php on line 7 A: 1; B: 2; C: Notice: Undefined offset: 2 in example.php on line 7 A: 3; B: 4; C:

Changelog

Version Description
7.0.0 foreach does not use the internal array pointer anymore.
5.5.0 Referencing of $value is supported for expressions. Formerly, only variables have been supported.
5.5.0 Unpacking nested arrays with list() is supported.

9 years ago

For those who"d like to traverse an array including just added elements (within this very foreach), here"s a workaround:

$values = array(1 => "a" , 2 => "b" , 3 => "c" );
while (list($key , $value ) = each ($values )) {
echo " $key => $value \r\n" ;
if ($key == 3 ) {
$values [ 4 ] = "d" ;
}
if ($key == 4 ) {
$values [ 5 ] = "e" ;
}
}
?>
the code above will output:

1 => a
2 => b
3 => c
4 => d
5 => e

6 years ago

I want to add some inline comments to dtowell"s piece of code about the iteration by reference:

$a = array("abe" , "ben" , "cam" );

foreach ($a as $k =>& $n )
$n = strtoupper ($n );

# At the end of this cycle the variable $n refers to the same memory as $a
# So when the second "foreach" assigns a value to $n:

Foreach ($a as $k => $n ) // notice NO reference here!
echo " $n \n" ;

# it is also modifying $a .
# So on the three repetitions of the second "foreach" the array will look like:
# 1. ("abe","ben","abe") - assigned the value of the first element to the last element
# 2. ("abe","ben","ben") - assigned the value of the second element to the last element
# 3. ("abe","ben","ben") - assigned the value of the third element to itself

Print_r ($a );
?>

4 years ago

Modifying array while foreach"ing it(yeah, such slime code;-)
if elements were added on last iteration or into array with 1 element, then added elements wont be iterated as foreach checks for pointer before iteration cycle
so it just quit and added elements wont be treated

2 years ago

I want just to mention that John is not entirely true.

Simple field test:

$m = microtime(1); $array = range(1,1000000); foreach ($array as &$i) { $i = 4; } echo microtime(1) - $m;

Result: 0.21731400489807

$m = microtime(1); $array = range(1,1000000); foreach ($array as $k => $i) { $array[$k] = 4; } echo microtime(1) - $m;

Result: 0.51596283912659

PHP Version: PHP 5.6.30 (cli) (built: Jan 18 2017 19:47:36)

Conclusion: Working with reference, although a bit dangerous is >2 times faster. You just need to know well what are you doing.

Best of luck and happy coding all

5 years ago

Foreach by reference internally deleted and created a new reference in each iteration, so it is not possible to directly use this value as a variable parameter values​​, look at the following example where the problem is observed and a possible solution:

class test
{
private $a = false ;
private $r = null ;
public function show (& $v )
{
if(! $this -> a )
{
$this -> a = true ;
$this -> r = & $v ;
}
var_dump ($this -> r );
}
public function reset ()
{
$this -> a = false ;
}
}

$t = new test ();

$a = array(array(1 , 2 ),array(3 , 4 ),array(5 , 6 ));
foreach($a as & $p )
$t -> show ($p );

/* Output obtain:
array (size=2)
0 => int 1
1 => int 2
array (size=2)
0 => int 1
1 => int 2
array (size=2)
0 => int 1
1 => int 2
*/

$t -> reset ();
foreach($a as $p )
{
$b = & $p ;
$t -> show ($b );
}

/* Output obtain:
array (size=2)
0 => int 1
1 => int 2
array (size=2)
0 => int 3
1 => int 4
array (size=2)
0 => int 5
1 => int 6
*/

4 years ago

String keys of associative arrays, for which is_numeric() is true and which can be type-juggled to an int will be cast to an int! If the key is on the other hand a string that can be type-juggled into a float, it will stay a string. (Observed on PHP 7.0.0RC8)

$arr = array();
$arr [ 0 ] = "zero" ; // will stay an int
$arr [ "1" ] = "one" ; // will be cast to an int !
$arr [ "two" ] = "2" ; // will stay a string
$arr [ "3.5" ] = "threeandahalf" ; // will stay a string

Foreach($arr as $key => $value ) {
var_dump ($key );
}
?>

The output will be

int(0)
int(1)
string(3) "two"
string(3) "3.5"

2 years ago

Foreach retains the state of internal defined variable:

/**
Result for this array is:
Hello World
Hello World
Hello World
*/
$arr = [ "a" , "b" , "c" ];
$title = "" ;
foreach ($arr as $r ) {
if ($r == "a" ) {
$title = "Hello World" ;
}
echo $title . "
" ;
}
?>

in this case, all we need to do is to add an else statement:
$arr = [ "a" , "b" , "c" ];
$title = "" ;
/**
This prints:
Hello World
*/
foreach ($arr as $r ) {
if ($r == "a" ) {
$title = "Hello World" ;
} else {
$title = "" ;
}
echo $title . "
" ;
}
?>

8 years ago

$d3 = array("a" =>array("b" => "c" ));
foreach($d3 [ "a" ] as & $v4 ){}
foreach($d3 as $v4 ){}
var_dump ($d3 );
?>
will get something look like this:
array(1) {
["a"]=>
array(1) {
["b"]=>
&array(1) {
["b"]=>
*RECURSION*
}
}
}
then you try to walk some data with this array.
the script run out of memory and connect reset by peer

the document says:
Warning
Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().

so what I learn is that NEVER ignore """Warning""" in document....

4 years ago

Just a simple strange behavior I have ran into:

If you accidentally put a semicolon after the foreach statement, you get no errors, but the loop will only run on the last element of the array:
$array = array(1 , 2 , 3 );
foreach ($array as $key );
{
echo $key ;
}
// output: 3
?>

Correctly:
$array = array(1 , 2 , 3 );
foreach ($array as $key )
{
echo $key ;
}
// output: 123
?>

It took me a while to find that semicolon.

(PHP 4, PHP 5, PHP 7)

Конструкция foreach предоставляет простой способ перебора массивов. Foreach работает только с массивами и объектами, и будет генерировать ошибку при попытке использования с переменными других типов или неинициализированными переменными. Существует два вида синтаксиса:

foreach (array_expression as $value) statement foreach (array_expression as $key => $value) statement

Первый цикл перебирает массив, задаваемый с помощью array_expression . На каждой итерации значение текущего элемента присваивается переменной $value и внутренний указатель массива увеличивается на единицу (таким образом, на следующей итерации цикла работа будет происходить со следующим элементом).

Второй цикл будет дополнительно соотносить ключ текущего элемента с переменной $key на каждой итерации.

Замечание :

Когда оператор foreach начинает исполнение, внутренний указатель массива автоматически устанавливается на первый его элемент Это означает, что нет необходимости вызывать функцию reset() перед использованием цикла foreach .

Так как оператор foreach опирается на внутренний указатель массива, его изменение внутри цикла может привести к непредсказуемому поведению.

Для того, чтобы напрямую изменять элементы массива внутри цикла, переменной $value должен предшествовать знак &. В этом случае значение будет присвоено по ссылке .

$arr = array(1 , 2 , 3 , 4 );
foreach ($arr as & $value ) {
$value = $value * 2 ;
}
// массив $arr сейчас таков: array(2, 4, 6, 8)
unset($value ); // разорвать ссылку на последний элемент
?>

Указатель на $value возможен, только если на перебираемый массив можно ссылаться (т.е. если он является переменной). Следующий код не будет работать:

foreach (array(1 , 2 , 3 , 4 ) as & $value ) {
$value = $value * 2 ;
}
?>

Внимание

Ссылка $value на последний элемент массива остается даже после того, как оператор foreach завершил работу. Рекомендуется уничтожить ее с помощью функции unset() .

Замечание :

Оператор foreach не поддерживает возможность подавления сообщений об ошибках с помощью префикса "@".

Вы могли заметить, что следующие конструкции функционально идентичны:


reset ($arr );
while (list(, $value ) = each ($arr )) {
echo "Значение: $value
\n" ;
}

foreach ($arr as $value ) {
echo "Значение: $value
\n" ;
}
?>

Следующие конструкции также функционально идентичны:

$arr = array("one" , "two" , "three" );
reset ($arr );
while (list($key , $value ) = each ($arr )) {

\n" ;
}

foreach ($arr as $key => $value ) {
echo "Ключ: $key ; Значение: $value
\n" ;
}
?>

Вот еще несколько примеров, демонстрирующие использование оператора:

/* Пример 1: только значение */

$a = array(1 , 2 , 3 , 17 );

foreach ($a as $v ) {
echo "Текущее значение переменной \$a: $v .\n" ;
}

/* Пример 2: значение (для иллюстрации массив выводится в виде значения с ключом) */

$a = array(1 , 2 , 3 , 17 );

$i = 0 ; /* только для пояснения */

Foreach ($a as $v ) {
echo "\$a[ $i ] => $v .\n" ;
$i ++;
}

/* Пример 3: ключ и значение */

$a = array(
"one" => 1 ,
"two" => 2 ,
"three" => 3 ,
"seventeen" => 17
);

foreach ($a as $k => $v ) {
echo "\$a[ $k ] => $v .\n" ;
}

/* Пример 4: многомерные массивы */
$a = array();
$a [ 0 ][ 0 ] = "a" ;
$a [ 0 ][ 1 ] = "b" ;
$a [ 1 ][ 0 ] = "y" ;
$a [ 1 ][ 1 ] = "z" ;

foreach ($a as $v1 ) {
foreach ($v1 as $v2 ) {
echo " $v2 \n" ;
}
}

/* Пример 5: динамические массивы */

Foreach (array(1 , 2 , 3 , 4 , 5 ) as $v ) {
echo " $v \n" ;
}
?>

Распаковка вложенных массивов с помощью list()

(PHP 5 >= 5.5.0, PHP 7)

В PHP 5.5 была добавлена возможность обхода массива массивов с распаковкой вложенного массива в переменные цикла, передав list() в качестве значения.