listDescriptionThis is perhaps the strangest construct (it's not really a function) in the PHP language; it's the only one that's intended to appear on the left side of an assignment statement. To use list() , you place a comma-separated list of variables (which need not exist beforehand) into the argument list, and then assign the value of an array to list() . Each variable listed is then populated with the value of the corresponding element from the array. If there are more elements in the array than variables listed, the extra elements are ignored. If there are more variables listed than elements in the array, a warning is generated. If no value is assigned to list() - that is, it's not on the left side of an assignment statement - a parse error is generated and script execution is terminated. If the value on the right side of the assignment statement is not an array, nothing happens. Elements of the array can be skipped by placing commas into the argument list with no variable between them. ExampleExample 51. Populate variables with values from an array /* Get all values into variables. */ $array = array('Bob', 'Doug', 'Stompin\' Tom'); list($bob, $doug, $stom) = $array; echo "\n$bob, $doug, $stom\n"; /* Just reset these... */ $bob = $doug = $stom = ''; /* Skip the middle element. $doug will be empty. */ list($bob, , $stom) = $array; echo "\n$bob, $doug, $stom\n\n"; /* Typical example of using list() and each() to iterate over an array */ while (list($key, $value) = each($array)) { echo "$key => $value\n"; } Output: Bob, Doug, Stompin' Tom Bob, , Stompin' Tom 0 => Bob 1 => Doug 2 => Stompin' Tom
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|