Each () returns 4 values, and the list assigns which two
Beginner PHP, ask each () return 4 values, list assignment which two
$prices = Array (' Tires ' = +, ' oil ' =, ' Spark plugs ' = 4);
$item = each ($prices);
Print_r ($item);
echo "
";
Reset ($prices);
while (list ($a, $b) = each ($prices)) {
echo "A: $a, B: $b
";
}
?>
The value of each ($prices) is an array of 4 key value,
Array ([1] = [value] = [0] = = Tires [key] = Tires)
The list ($a, $b) = each ($prices)) is why $item[key], $item [value] is assigned a value of $ A, $b, not $item[1], $item [value].
The list should not be assigned in order.
Online search, official documents and documents have also been found, there is no accurate answer, you can solve the amount of students each List
Share to:
------Solution--------------------
There are two possibilities you didn't see.
1.
List () only works on numerical arrays and assumes the numerical indices start at 0.
That is, the priority is to use the value of the numeric index.
2.
List () assigns the values starting with the Right-most parameter.
From the far right of the parameter list, so $b is assigned a value of 0 indexed, that is, tires, and then $ A is assigned a value of 1 indexed, so it is 100.
------Solution--------------------
Observe the following facts:
List ($a, $b) = Array (' A ', ' B ');
echo "A: $a B: $b \ n"; Output A:A b:b
List ($a, $b) = Array (1 = ' a ', 0 = ' B ');
echo "A: $a B: $b \ n"; Output A:B b:a
List ($a, $b) = Array (' a ' = = ' A ', ' B ' = ' B ');
echo "A: $a B: $b \ n"; Output A:B:
Error message:
The list is to extract the corresponding subscript element from the array in sequence by the ordinal of the argument list.
That is, the value of the variable in the list is the value of the array underlying, regardless of the order in which they are listed in the array
So what is the point of "assigning from the far right of the parameter list"?
Please look at this writing.
List ($a [$b] [$c], $b, $c) = Array (' A ', ' B ', ' C ');
Print_r ($a);
Array
(
[b] = = Array
(