Each () returns four values. which two values are assigned to the list?

Source: Internet
Author: User
Each () returns four values. which two each lists are assigned by list?

Php beginners. Can I ask which two values are returned by each () and assigned by list?
$ Prices = array ('tires' => 100, 'Oil '=> 10, 'spark S s' => 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 four key values,
Array ([1] => 100 [value] => 100 [0] => Tires [key] => Tires)

Why does list ($ a, $ B) = each ($ prices) assign $ item [key] and $ item [value] to $ a, $ B, instead of $ item [1], $ item [value].
Shouldn't a list be an ordered value assignment.
I found it online, and I also found the official documentation. I don't have any accurate answers.


Reply to discussion (solution)

You may not see either of them.
1.
List () only works on numerical arrays and assumes the numerical indices start at 0.
That is, the value of the numeric index is used first.
2.
List () assigns the values starting with the right-most parameter.
Starting from the rightmost of the parameter list, $ B is assigned a value of 0 index, that is, Tires. then $ a is assigned a value of 1 index, so it is 100.

Observe the following facts:

List ($ a, $ B) = array ('A', 'B'); echo "a: $ a B: $ B \ n"; // output: a B: blist ($ a, $ B) = array (1 => 'A', 0 => 'B'); echo "a: $ a B: $ B \ n "; // output a: B: alist ($ a, $ B) = array ('a' => 'A ', 'B' => 'B'); echo "a: $ a B: $ B \ n"; // output a: B: Error message: Undefined offset: 1 Undefined offset: 0

It can be seen that list extracts the corresponding subscript element from the array sequentially by the serial number of the parameter list.
That is, the value of the variable in the list is the value of the lower mark of the array, but it is irrelevant to the order of the lower mark in the array.

So what is the significance of "assigning values from the rightmost of the parameter list?
Please refer to this writing method
list($a[$b][$c], $b, $c) = array('a', 'b', 'c');print_r($a);
Array
(
[B] => Array
(
[C] =>
)
)
Because $ c and $ B are assigned a value before $ a, no error occurs when you assign a value to $ a [$ B] [$ c ].

Thanks to the above two students,
I did not read this sentence,
List () only works on numerical arrays and assumes the numerical indices start at 0.
List () only works on the array of values whose index starts from 0

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.