documentation on website of list vs. each
| From: | Dennis Gearon | Date: | Fri, 02 May 2003 15:43:19 +0000 |
| Subject: | documentation on website of list vs. each | ||
| Groups: | php.doc | ||
| Request: | Send a blank email to phpdoc+get-969353029@lists.php.net to get a copy of this message | ||
The documentation of each() says:
array each ( array array)
--------------------------
Returns the current key and value pair from the array array and
advances the array cursor. This pair is returned in a
four-element array, with the keys 0, 1, key, and value.
Elements 0 and key contain the key name of the array element,
and 1 and value contain the data.
The documentation of list() says:
Warning
--------
list() assigns the values starting with the right-most parameter.
If you are using plain variables, you don't have to worry about this.
But if you are using arrays with indices you usually expect the order
of the indices in the array the same you wrote in the list() from
left to right; which it isn't. It's assigned in the reverse order.
=============================================================================
To me, that says that each will issue an array with the element [0] containing the key of an array postion, and the element[1] containing the value of an
array position.
And so, using list on the array generated by each should assign the key to the
rightmost variable in the list and the value to the next variable to the right.
=============================================================================
If you try this, it will print out opposite of what you expect. It prints out
the keys to the array '$keys', not the values.
$event_fields_arr = array(
"event_id" =>"",
"usr_name" =>"",
"usr_email" =>"",
"centennial_name" =>"",
"event_name" =>"",
"event_category" =>"",
"event_link" =>"",
"event_cost" =>"",
"event_phone" =>"",
"event_begin_day" =>"",
"event_begin_month" =>"",
"event_begin_year" =>"",
"event_end_day" =>"",
"event_end_month" =>"",
"event_end_year" =>"",
"event_sched" =>"",
"event_desc" =>"",
"event_addr" =>"",
"event_city" =>"",
"event_state" =>"",
"event_zip" =>"",
"company_name" =>"",
"company_addr" =>"",
"company_city" =>"",
"company_state" =>"",
"company_zip" =>""
);
$keys = array_keys( $event_fields_arr );
while( list ($val, $key) = each( $keys ) ){
echo( $val . "<br>" );
}