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