PHP 5.6.0beta1 released

list

(PHP 4, PHP 5)

listWeist Variablen zu, als wären sie ein Array

Beschreibung

void list ( mixed $varname , mixed $... )

Wie array() ist auch dies keine wirkliche Funktion, sondern ein Sprachkonstrukt. list() wird verwendet, um eine Liste von Variablen innerhalb einer Operation zuzuweisen.

Hinweis:

list() funktioniert nur bei numerischen Arrays und basiert auf der Annahme, dass die numerischen Indizes bei 0 beginnen.

Beispiel #1 list()

<?php

$info 
= array('Kaffee''braun''Koffein');

// Auflisten aller Variablen
list($drink$color$power) = $info;
echo 
"$drink ist $color und $power macht es zu etwas besonderem.\n";

// Ein paar davon auflisten
list($drink, , $power) = $info;
echo 
"$drink hat $power.\n";

// Oder nur die dritte ausgeben
list( , , $power) = $info;
echo 
"Ich brauche $power!\n";

?>

Beispiel #2 list()

<table>
 <tr>
  <th>Mitarbeitername</th>
  <th>Gehalt</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>

Warnung

list() weist die Werte von rechts beginnend zu. Wenn Sie einfache Variablen benutzen, brauchen Sie sich nicht darum zu kümmern. Wenn Sie jedoch Arrays mit Indizes verwenden, erwarten Sie gewöhnlich die Reihenfolge der Indizes in dem Array genau so, wie Sie sie in list() geschrieben haben (von links nach rechts), was jedoch nicht der Fall ist. Es wird in der umgekehrten Reihenfolge zugewiesen.

Beispiel #3 list() mit Array Indizes verwenden

<?php

$info 
= array('coffee''brown''caffeine');

list(
$a[0], $a[1], $a[2]) = $info;

var_dump($a);

?>

Die Ausgabe (Beachten Sie die Reihenfolge der Elemente, verglichen mit der in list() eingetragenen Reihenfolge):

array(3) {
  [2]=>
  string(8) "caffeine"
  [1]=>
  string(5) "brown"
  [0]=>
  string(6) "coffee"
}

Siehe auch each(), array() und extract().

add a note add a note

User Contributed Notes 10 notes

up
24
chris at chlab dot ch
1 year ago
The example states the following:
<?php
// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar);
// output: NULL
?>

If the string is in a variable however, it seems using list() will treat the string as an array:
<?php
$string
= "abcde";
list(
$foo) = $string;
var_dump($foo);
// output: string(1) "a"
?>
up
11
megan at voices dot com
7 months ago
As noted, list() will give an error if the input array is too short. This can be avoided by array_merge()'ing in some default values. For example:

<?php
$parameter
= 'name';
list(
$a, $b ) = array_merge( explode( '=', $parameter ), array( true ) );
?>

However, you will have to array_merge with an array long enough to ensure there are enough elements (if $parameter is empty, the code above would still error).

An alternate approach would be to use array_pad on the array to ensure its length (if all the defaults you need to add are the same).

<?php
    $parameter
= 'bob-12345';
    list(
$name, $id, $fav_color, $age ) = array_pad( explode( '-', $parameter ), 4, '' );
   
var_dump($name, $id, $fav_color, $age);
/* outputs
string(3) "bob"
string(5) "12345"
string(0) ""
string(0) ""
*/
?>
up
16
svennd
1 year ago
The list() definition won't throw an error if your array is longer then defined list.
<?php

list($a, $b, $c) = array("a", "b", "c", "d");

var_dump($a); // a
var_dump($b); // b
var_dump($c); // c
?>
up
0
Arne
8 months ago
list() will give an error if the input array is too short. This can be avoided by array_merge()'ing in some default values. For example:

<?php
$parameter
= 'name';
list(
$a, $b ) = array_merge( explode( '=', $parameter ), array( true ) );
?>
up
0
Achilles at thegreatwarrior dot com
9 months ago
Second, when you’re using the list() function, you must acknowledge each array element. You could not do this
list($weekday, $month) = $date;

But you can use empty values to ignore elements:
list ($weekday, , $month) = $date;
up
-1
Thanos K.
1 month ago
Also it seems that it doesn't work as expected with arrays with non numeric keys:

list($k, $l, $m) = array('a' => 'val1', 'b' => 'val2', 'c' => 'val3');

Gives empty variables..
up
0
srikanth at networthindia dot com
1 year ago
Note: list cannot assign array cast of object to variables straight away. first you need to convert the object to numeric indexed array.

ex:
list($a, $b, $d) = (array) $abc; // $abc is an object; this will not assign.
list($a, $b, $c) = array_values((array) $abc); // This will work.
up
-2
Matt
10 months ago
You can't type check within the list() parameters:

list ( array $var1, $var2 ) = array ( array('one','two'), 'three');

generates a parse error, unexpected 'array'.
up
-2
edam
2 months ago
This doesn't work on associative array.  For example:

    list( $a, $b, $c ) = array( 'a' => 'a', 'b' => 'b', 'c' => 'c' );
    PHP Notice:  Undefined offset: 2 in Command line code on line 1
    PHP Notice:  Undefined offset: 1 in Command line code on line 1
    PHP Notice:  Undefined offset: 0 in Command line code on line 1
up
-2
john at jbwalker dot com
4 months ago
The list construct seems to look for a sequential list of indexes rather taking elements in sequence. What that obscure statement means is that if you unset an element, list will not simply jump to the next element and assign that to the variable but will treat the missing element as a null or empty variable:

    $test = array("a","b","c","d");
    unset($test[1]);
    list($a,$b,$c)=$test;
    print "\$a='$a' \$b='$b' \$c='$c'<BR>";

results in:
$a='a' $b='' $c='c'

not:
$a='a' $b='c' $c='d'
To Top