Each () returns four values. which two php beginners are assigned the list value? can you tell me that each () & nbsp; returns four values, & nbsp; list & nbsp; which two values are assigned ;? Php $ prices & nbsp ;=& nbsp; array ('tires' & nbsp ;=& gt; & nbsp; each () returns four values. which two values are assigned to the 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 didn't have any accurate answers. I shared my answers to the each list :? 100 ,? 'Oil '? =>? 10 ,? 'Spa... 'data-pics = ''>
------ 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.
------ 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:
List ($ 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
(