158 of 410 menu

The list Function

The list function extracts array elements into variables.

Syntax

list(mixed $var, mixed ...$vars): array

Example

Let's extract array elements into variables:

<?php $arr = [1, 2, 3, 4, 5]; list($a, $b, $c, $d, $e) = $arr; ?>

As a result, we will have access to variable $a, variable $b, variable, variable , variable $d, variable $e:

<?php echo $a; // outputs 1 echo $b; // outputs 2 echo $c; // outputs 3 echo $d; // outputs 4 echo $e; // outputs 5 ?>

Example

If you need to start not from the first element of the array - you can put a comma:

<?php $arr = [1, 2, 3, 4, 5]; list(, $b, $c, $d, $e) = $arr; ?>

As a result, we will have access to variable $b, variable $c, variable $d and variable $e:

<?php echo $b; // outputs 2 echo $c; // outputs 3 echo $d; // outputs 4 echo $e; // outputs 5 ?>

Example

Let's write only the third element of the array into a variable:

<?php $arr = [1, 2, 3, 4, 5]; list(, , $c) = $arr; ?>

As a result, we will have access to variable $c:

<?php echo $c; // outputs 3 ?>

See Also

  • the extract function,
    which splits an array into variables
byenru