list
Assign values from an array to a set of variables
list()
function is used to assign values to a set of variables in a single operation.
Note: This function is only used for arrays of numeric indexes, and assumes that numeric indexes start at 0.
Assign the values in the array to some variables:
<?php $my_array = array ( "Dog" , "Cat" , "Horse" ) ; list ( $a , $b , $c ) = $my_array ; echo "I have several animals, a $a , a $b and a $c ." ; ?>
Try it yourself
Use the first and third variables:
<?php $my_array = array ( "Dog" , "Cat" , "Horse" ) ; list ( $a , , $c ) = $my_array ; echo "I only use the $a and $c variables here." ; ?>
Try it yourself
list ( var1 , var2 ... )
parameter | describe |
---|---|
var1 | Required. The first variable that needs to be assigned. |
var2 ,... | Optional. More variables that need to be assigned. |
list()
function uses elements in the array to assign values to a set of variables.
Note that similar to array() , list()
is actually a language structure, not a function.