Assign variables as if they were an array (original) (raw)
list
(PHP 4, PHP 5, PHP 7)
list — Assign variables as if they were an array
Description
list ( mixed $var1
[, mixed $...
] ) : array
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.
Parameters
var1
A variable.
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. |
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. |
Examples
Example #1 list() examples
`<?php
$info
= array('coffee', 'brown', 'caffeine');// Listing all the variables
list($drink, color,color, color,power) = $info;
echo "$drink is colorandcolor and colorandpower makes it special.\n";// Listing some of them
list($drink, , power)=power) = power)=info;
echo "$drink has $power.\n";// Or let's skip to only the third one
list( , , power)=power) = 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()
`
Employee name | Salary |
---|---|
$name | \n" . "$salary | \n" . "
`
Example #3 Using nested list()
<?phplist($a, list($b, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo>=</mo><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><mi>y</mi><mo stretchy="false">(</mo><mn>1</mn><mo separator="true">,</mo><mi>a</mi><mi>r</mi><mi>r</mi><mi>a</mi><mi>y</mi><mo stretchy="false">(</mo><mn>2</mn><mo separator="true">,</mo><mn>3</mn><mo stretchy="false">)</mo><mo stretchy="false">)</mo><mo separator="true">;</mo><mi>v</mi><mi>a</mi><msub><mi>r</mi><mi>d</mi></msub><mi>u</mi><mi>m</mi><mi>p</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">c)) = array(1, array(2, 3));var_dump(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">c</span><span class="mclose">))</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">rr</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mopen">(</span><span class="mord">1</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">rr</span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mopen">(</span><span class="mord">2</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">3</span><span class="mclose">))</span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0278em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">d</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">u</span><span class="mord mathnormal">m</span><span class="mord mathnormal">p</span><span class="mopen">(</span></span></span></span>a, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">b, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">b</span><span class="mpunct">,</span></span></span></span>c);?>
Example #4 Using list() with array indices
`<?php
$info
= array('coffee', 'brown', 'caffeine');
list( a[0],a[0], a[0],a[1], a[2])=a[2]) = 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 bylist() are defined is irrelevant.
<?php $foo = array(2 => 'a', 'foo' => 'b', 0 => 'c'); $foo[1] = 'd'; list($x, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>y</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">y, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mpunct">,</span></span></span></span>z) = $foo; var_dump($foo, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>x</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">x, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">x</span><span class="mpunct">,</span></span></span></span>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" => <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mi>d</mi><mo separator="true">,</mo><mi mathvariant="normal">"</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mi mathvariant="normal">"</mi><mo>=</mo><mo>></mo></mrow><annotation encoding="application/x-tex">id, "name" => </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">i</span><span class="mord mathnormal">d</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">"</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mord">"</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=></span></span></span></span>name]) { echo "id: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>i</mi><mi>d</mi><mo separator="true">,</mo><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi><mo>:</mo></mrow><annotation encoding="application/x-tex">id, name: </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">i</span><span class="mord mathnormal">d</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span></span></span></span>name\n"; } echo PHP_EOL; list(1 => <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>e</mi><mi>c</mi><mi>o</mi><mi>n</mi><mi>d</mi><mo separator="true">,</mo><mn>3</mn><mo>=</mo><mo>></mo></mrow><annotation encoding="application/x-tex">second, 3 => </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">seco</span><span class="mord mathnormal">n</span><span class="mord mathnormal">d</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">3</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=></span></span></span></span>fourth) = [1, 2, 3, 4]; echo "$second, $fourth\n";
The above example will output:
id: 1, name: Tom id: 2, name: Fred
2, 4