Current Location: Home> Function Categories> list

list

Assign values ​​from an array to a set of variables
Name:list
Category:Array
Programming Language:php
One-line Description:Assign the values ​​in the array to some variables.

Definition and usage

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.

Example

Example 1

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

Example 2

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

grammar

 list ( var1 , var2 ... )
parameter describe
var1 Required. The first variable that needs to be assigned.
var2 ,... Optional. More variables that need to be assigned.

illustrate

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.

Similar Functions
Popular Articles