22 Votes

PHP: Write array to individual Variables

Tutorial by Stefan Trost | Last update on 2023-01-27 | Created on 2014-03-19

Today, I would like to show you an easy way of how to store the content of an array in single variables uncomplicated and quickly.

For this, we are using the PHP function list():

$arr = array(1, 2, 3);
list($var1, $var2, $var3) = $arr;

The example above shows how to use this function. First, we are creating an array $arr containing 3 elements (1, 2 and 3). After that, we are using list() to write those elements into the variables $var1, $var2 and $var3. The order of the variables corresponds to the order of the elements in the array.

This example creates the same result like the following code - but it is shorter:

$arr = array(1, 2, 3);
$var1 = $arr[0];
$var2 = $arr[1];
$var3 = $arr[2];

Especially when dealing with long arrays, the code with list() can be a lot shorter and more easily and faster to write.

Database Queries with list()

A practical example for the use of list() is reading a database query.

while (list($col1, $col2) = mysqli_fetch_row($res)) {
  echo "$col1 $col2";
}

When we have retrieved some lines with multiple columns from a MySQL database, we can easily use mysqli_fetch_row() to go through the different lines of our result. The function mysqli_fetch_row() returns an arrays for each current line which we can divide into individual variables with list().

Assign only individual Values from an Array to Variables

Incidentally, if we do not need all values from our array, we are not forced to assign variables that we do not use later. In this case, the unnecessary elements or areas from the array can just be omitted in the list of variables passed as parameters to list(). That could look like this, for example:

$arr = array(1, 2, 3);
list($var1, , $var3) = $arr;

In this example, we only assign the first and the third array element to our variables $var1 and $var3. In order to write only the third value into a variable, we could also use "list(,,$var3)", whereby in this case "$var3 = $arr[2]" would not be much more to write.

How to use list() with nested Arrays

List() does not only work for simple one-dimensional arrays. List() can also process and transform multi-dimensional nested arrays into variables. Let's look at an example also for such a case:

$arr = array(1, array('a', 'b'), 3, array(1, 2, 3));
list($var1, list($var2a, $var2b), $var3, list($var4a, $var4b, $var4c)) = $arr;

Here we set $arr to an array consisting of individual values as well as arrays with different element types. By using list() as nested as the array is built up, we also reach these "inner" values.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.