Geen cache-versie.

Caching uitgeschakeld. Standaardinstelling voor deze pagina:ingeschakeld (code LNG204)
Als het scherm te langzaam is, kunt u de gebruikersmodus uitschakelen om de cacheversie te bekijken.

Rechercher une fonction PHP

list

(PHP 4, PHP 5, PHP 7)

listAssign variables as if they were an array

Description

list ( mixed $var1 [, mixed $... ] ) : array

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:

Before PHP 7.1.0, list() only worked on numerical arrays and assumes the numerical indices start at 0.

Warning

In PHP 5, list() assigns the values starting with the right-most parameter. In PHP 7, list() starts with the left-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 is not the case in PHP 5, as it's assigned in the reverse order.

Generally speaking, it is advisable to avoid relying on a specific order of operation, as this may change again in the future.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Parameters

var1

A variable.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Return Values

Returns the assigned array.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Changelog

Version Description
7.1.0 It is now possible to specify keys in list(). This enables destructuring of arrays with non-integer or non-sequential keys.
7.0.0 The order that the assignment operations are performed in has changed.
7.0.0 list() expressions can no longer be completely empty.
7.0.0 Strings can no longer be unpacked.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Examples

Example #1 list() examples

<?php

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

// Listing all the variables
list($drink$color$power) = $info;
echo 
"$drink is $color and $power makes it special.\n";

// Listing some of them
list($drink, , $power) = $info;
echo 
"$drink has $power.\n";

// Or let's skip to only the third one
list( , , $power) = $info;
echo 
"I need $power!\n";

// list() doesn't work with strings
list($bar) = "abcde";
var_dump($bar); // NULL
?>

Example #2 An example use of list()

<table>
 <tr>
  <th>Employee name</th>
  <th>Salary</th>
 </tr>

<?php
$result 
$pdo->query("SELECT id, name, salary FROM employees");
while (list(
$id$name$salary) = $result->fetch(PDO::FETCH_NUM)) {
    echo 
" <tr>\n" .
          
"  <td><a href=\"info.php?id=$id\">$name</a></td>\n" .
          
"  <td>$salary</td>\n" .
          
" </tr>\n";
}

?>

</table>

Example #3 Using nested list()

<?php

list($a, list($b$c)) = array(1, array(23));

var_dump($a$b$c);

?>
int(1)
int(2)
int(3)

Example #4 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):

Output of the above example in PHP 7:

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

Output of the above example in PHP 5:

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

Example #5 list() and order of index definitions

The order in which the indices of the array to be consumed by list() are defined is irrelevant.

<?php
$foo 
= array(=> 'a''foo' => 'b'=> 'c');
$foo[1] = 'd';
list(
$x$y$z) = $foo;
var_dump($foo$x$y$z);

Gives the following output (note the order of the elements compared in which order they were written in the list() syntax):

array(4) {
  [2]=>
  string(1) "a"
  ["foo"]=>
  string(1) "b"
  [0]=>
  string(1) "c"
  [1]=>
  string(1) "d"
}
string(1) "c"
string(1) "d"
string(1) "a"

Example #6 list() with keys

As of PHP 7.1.0 list() can now also contain explicit keys, which can be given as arbitrary expressions. Mixing of integer and string keys is allowed; however, elements with and without keys cannot be mixed.

<?php
$data 
= [
    [
"id" => 1"name" => 'Tom'],
    [
"id" => 2"name" => 'Fred'],
];
foreach (
$data as ["id" => $id"name" => $name]) {
    echo 
"id: $id, name: $name\n";
}
echo 
PHP_EOL;
list(
=> $second=> $fourth) = [1234];
echo 
"$second$fourth\n";

The above example will output:

id: 1, name: Tom
id: 2, name: Fred

2, 4

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

See Also

  • each() - Return the current key and value pair from an array and advance the array cursor
  • array() - Create an array
  • extract() - Import variables into the current symbol table from an array

Zoek een PHP-functie

Vertaling niet beschikbaar

De PHP-handleiding is nog niet in het Nederlands vertaald, dus het scherm is in het Engels. Als u wilt, kunt u het ook in het Frans of in het Duits raadplegen.

Als je de moed voelt, kun je je vertaling aanbieden ;-)

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 30/01/2003 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/php-rf-function.list.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:fr Manuel PHP : http://php.net

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut