The array is an important part of the PHP and other programming languages. Arrays can store multiple items in a single structure or variable. Creatin an array from multiple items are a well-known topic but converting an array into multiple variables or items is a less known topic. The list() method can be used to convert an array into multiple variables in PHP. The list() method can be used in PHP 4.0, PHP 5.0 and PHP 7.0 versions. But before PHP 7.1.0 list() method can be only used with numerical arrays and indices starts from 0.
PHP list() Method Syntax
The list() function has a bit different syntax where the variables are put inside the function and the array will provided on the right side with equal sign.
list( $v1, $v2, ...) = $arr;
- $arr is the array that contains all items.
- $v1, $v2, … are variables listed in a row. The sequence is important where the first $array item will be put inside $v1, the second $array1 item will be put inside the $v2 and more items can be set which is related to the $array1 length or item count. The variables should be equal with the $array1 count or size where ,, can be used unset $array1 items which will be examined below.
Convert String Array Into Multiple Variables
We will start with a simple example where we will create an array that contains 3 names. The array is named $arr and contains “ismail”,”ahmet”,”ali”. We will set into variables named $v1, $v2 and $v3. Keep in mind that string item conversion is available with the PHP version 7.1.0.
$arr = array("ismail","ahmet","ali");
list($v1,$v2,$v3) = $arr;
echo $v1;
echo $v2;
echo $v3;
Convert Integer Array Into Multiple Variables
Integer arrays can be also converted into variables by using the list() function. Below we will convert a simple array that contains 1,2,3 numbers into $v1, $v2, $v3.
$arr = array(1,2,3);
list($v1,$v2,$v3) = $arr;
echo $v1;
echo $v2;
echo $v3;
Skip Some Array Items
For simple arrays, we can define multiple variables but if the array has a lot of items we can skip some array items to assign a variable. This can be useful if we do not need all items to set a variable. We will use two item delimiter command like , , where between commas are used for an item which will not be assigned into a variable. In the following example, we do not put the $v2 as a list() function parameter and only use , , which will skip the array item after $v1 and before $v3.
$arr = array(1,2,3);
list($v1, ,$v3) = $arr;
echo $v1;
//1
echo $v3;
//3
We can also skip first or last array items like below and only set the $v2 .
$arr = array(1,2,3);
list(, $v2 ,) = $arr;
echo $v2;
//2
Convert Nested Array Into Multiple Variables
The list() function works with simple single level arrays without problem but will it work with the nested arrays which are multilevel arrays, arrays inside array is possible? Yes we will just nest the list function like nested arrays.
$arr = array(1,2,(3,4,5));
list($v1, $v2, list($v31,$v32,$v33)) = $arr;
echo $v1;
//1
echo $v2;
//2
echo $v31;
//3
echo $v32;
//4
echo $v33;
//5
Alternatively we can also skip some items or arrays like previously described with , , . Below we will skip an array and an item from nested array.
$arr = array((1,2,3),4,5,(6,7,8));
list( , $v2, , list($v31, ,$v33)) = $arr;
echo $v2;
//4
echo $v31;
//6
echo $v33;
//8
Convert Variables with Keys
With the PHP 7.1 keys can be used with the list() function in order to specify the key and get its value from the array. In this case the sequence is not important because variable assignment will be done according to the array item key.
$arr = array('one=>1, 'two'=>2, 'three'=>3);
list('two'=>$v2, 'three'=>$v3 , 'one'=>$v1) = $arr;
echo $v1;
//1
echo $v3;
//3