|
|
 |
list (PHP 3, PHP 4, PHP 5 ) list --
Assign variables as if they were an array
Descriptionvoid list ( mixed varname, mixed ... )
Like array(), this is not really a function,
but a language construct. list() is used to
assign a list of variables in one operation.
Note:
list() only works on numerical arrays and assumes
the numerical indices start at 0.
Example 1. list() examples |
<?php
$info = array('coffee', 'brown', 'caffeine');
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";
list($drink, , $power) = $info;
echo "$drink has $power.\n";
list( , , $power) = $info;
echo "I need $power!\n";
?>
|
|
Example 2. An example use of list() |
<table>
<tr>
<th>Employee name</th>
<th>Salary</th>
</tr>
<?php
$result = mysql_query("SELECT id, name, salary FROM employees", $conn);
while (list($id, $name, $salary) = mysql_fetch_row($result)) {
echo " <tr>\n" .
" <td><a href=\"info.php?id=$id\">$name</a></td>\n" .
" <td>$salary</td>\n" .
" </tr>\n";
}
?>
</table>
|
|
| 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.
|
Example 3. Using list() with array indices |
<?php
$info = array('coffee', 'brown', 'caffeine');
list($a[0], $a[1], $a[2]) = $info;
var_dump($a);
?>
|
Gives the following output (note the order of the elements compared in
which order they were written in the list() syntax):
array(3) {
[2]=>
string(8) "caffeine"
[1]=>
string(5) "brown"
[0]=>
string(6) "coffee"
} |
|
See also each(), array()
and extract().
User Contributed Notes
list
php at keithtyler dot com
12-May-2005 04:27
Note to perl programmers, this does NOT work like Perl's anonymous-list technique. It will discard any elements on the right hand side that do not have a corresponding recipient value on the left hand side.
In Perl, you can put an array at the end of the list, and it will pick up all remaining elements from the right hand of the assignment.
($val1,$val2,@others)=array("a","b","c","d","e")
will result in $val1="a", $val2="b", and @others=("c","d","e").
This cannot seem to be done in PHP, even if you declare the array beforehand:
$others=Array();
list($val1,$val2,$others)=array("a","b","c","d","e")
will result in $val1="a", $val2="b", and $others="c". Note also that $others is redefined as whatever type the right-hand side value was, and is no longer an Array.
This is in 4.3.11.
mortoray at ecircle-ag dot com
16-Feb-2005 03:29
There is no way to do reference assignment using the list function, therefore list assignment is will always be a copy assignment (which is of course not always what you want).
By example, and showing the workaround (which is to just not use list):
function &pass_refs( &$a ) {
return array( &$a );
}
$a = 1;
list( $b ) = pass_refs( $a ); //*
$a = 2;
print( "$b" ); //prints 1
$ret = pass_refs( $a );
$b =& $ret[0];
$a = 3;
print( "$b" ); //prints 3
*This is where some syntax like the following would be desired:
list( &$b ) = pass_refs( $a );
or maybe:
list( $b ) =& pass_refs( $a );
jennevdmeer at zonnet dot nl
21-Oct-2004 10:29
This is a function simulair to that of 'list' it lists an array with the 'key' as variable name and then those variables contain the value of the key in the array.
This is a bit easier then list in my opinion since you dont have to list up all variable names and it just names them as the key.
<?php
function lista($a) {
foreach ($a as $k => $v) {
$s = "global \$".$k;
eval($s.";");
$s = "\$".$k ." = \"". $v."\"";
eval($s.";");
}
}
?>
HW
14-Aug-2004 03:08
The list() construct can be used within other list() constructs (so that it can be used to extract the elements of multidimensional arrays):
<?php
$matrix = array(array(1,2),
array(3,4));
list(list($tl,$tr),list($bl,$br)) = $matrix;
echo "$tl $tr $bl $br";
?>
Outputs "1 2 3 4".
jeronimo at DELETE_THIS dot transartmedia dot com
28-Jan-2004 09:28
If you want to swap values between variables without using an intermediary, try using the list() and array() language constructs. For instance:
<?
$biggest = 1;
$smallest = 10;
$temp = $biggest;
$biggest = $smallest;
$smallest = $temp;
list($biggest, $smallest) = array($smallest, $biggest);
?>
This works with any number of variables; you're not limited to just two.
Cheers,
Jeronimo
rubein at earthlink dot net
28-Dec-2000 07:15
Note: If you have an array full of arrays, you can't use list() in conjunction to foreach() when traversing said array, e.g.
$someArray = array(
array(1, "one"),
array(2, "two"),
array(3, "three")
);
foreach($somearray as list($num, $text)) { ... }
This, however will work
foreach($somearray as $subarray) {
list($num, $text) = $subarray;
...
}
| |