Each () returns a value of 4, the list is assigned to which two

Source: Internet
Author: User
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
(
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.