list

(PHP 4, PHP 5, PHP 7, PHP 8)

listAssign variables as if they were an array

Description

list(mixed $var, mixed ...$vars = ?): 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. Strings can not be unpacked and list() expressions can not be completely empty.

Note:

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

Parameters

var

A variable.

vars

Further variables.

Return Values

Returns the assigned array.

Changelog

Version Description
7.3.0 Support for reference assignments in array destructuring was added.
7.1.0 It is now possible to specify keys in list(). This enables destructuring of arrays with non-integer or non-sequential keys.

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()

<?php
$result 
$pdo->query("SELECT id, name FROM employees");
while (list(
$id$name) = $result->fetch(PDO::FETCH_NUM)) {
    echo 
"id: $id, name: $name\n";
}
?>

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 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 #5 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

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

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial