As with other programming languages, PHP also has a way to unpack arrays and objects. In this blog, we will look at destructuring assignments in PHP7.
PHP Array Destructuring:
There are functions available in PHP that allow you to unpack array values. These are,
- List
- Extract
List
The List is not function but a language construct like an array. It helps assign array values to multiple variables at a time. Before php7.1.0, list() supported only a numerically indexed array, however it now supports arrays of non-integer or non-sequential keys (associative array). Also, the list() function has a shorthand syntax ([]).
The List function assigns values of an array on the right to a variable given as a parameter on the left side. Let’s see the use of the list function in array destructuring.
List function with a numerically indexed array
For numerically indexed arrays, the list function assumes that numerical indices start from 0 and that the keys are in sequence. While destructuring numerical keys array with the list function or shorthand [], the assignment of values to the variable is based on the index of values. The first value of the array will be assigned to the first variable given in the list function and so on.
Performing destructuring on a numerically indexed array using the list function
Unpack all values
By providing a variable name for each value in the array we can easily unpack the array values.
$hobbies = ["cricket", "carrom", "chess"]; // with list() function list($cricket, $carrom, $chess) = $hobbies; echo "I Love to play $cricket, $carrom and $chess"; // I Love to play cricket, carrom and chess // with shorthand [] [$cricket, $carrom, $chess] = $hobbies; echo "I Love to play $cricket, $carrom and $chess"; // I Love to play cricket, carrom and chess
Skip some values
To skip a value from the array does not state the variable declaration for that value. The list function will skip that particular value.
$hobbies = ["cricket", "carrom", "chess"]; // with list() function list($cricket, , $chess) = $hobbies; echo "I Love to play $cricket and $chess"; //I Love to play cricket and chess // with shorthand [] [$cricket, , $chess] = $hobbies; echo "I Love to play $cricket and $chess"; //I Love to play cricket and chess
Swap variables
This requires a third variable when swapping two values, but list() or [] shorthand does it for you without any temporary variable to store the values during the swapping process.
// with list() function $a = 10; $b = 20; list($a, $b) = [$b, $a]; echo $a."\n"; // 20 echo $b."\n"; //10 // with shorthand [] $a = 30; $b = 40; [$a, $b] = [$b, $a]; echo $a."\n"; //40 echo $b."\n"; //30
List function with an associative array
For an associative array, the list() function variable can be assigned only for the keys that are available in the array. Unlike a numerically indexed array, you can assign array values to variables in any sequence by their key name.
Performing destructuring on the associative array using a list,
Unpack all values
To unpack all available values, we just need to assign a variable to each key of that array.
$student = ["roll_num" => 001, "name" => "John", "class" => "1st"]; // with list() list("roll_num" => $roll_number, "name" => $name, "class" => $class) = $student; echo $roll_number."\n"; // 001 echo $name."\n"; // John echo $class."\n"; // 1st // with shorthand [] $student = ["roll_num" => 002, "name" => "Sam", "class" => "2nd"]; ["roll_num" => $roll_number, "name" => $name, "class" => $class] = $student; echo $roll_number."\n"; // 002 echo $name."\n"; // Sam echo $class."\n"; // 2nd
Assignment on the basis of the key name
Associative arrays do not need to maintain a variable assignment as per the array index. Key names can be used to assign variables for array values.
["class" => $class, "roll_num" => $roll_number, "name" => $name] = $student; echo $roll_number; // 002 echo $name; // Sam echo $class; // 2nd
Skip some values
By simply not specifying the key on the left-hand we can skip the values of arrays that we do not wish to unpack.
// with list() $numbers = ["a" => 10, "b" => 20, "c" => 30, "d" => 40]; list("a" => $a, "d" => $d) = $numbers; echo $a."\n"; // 10 echo $d."\n"; // 40 // with shorthand [] $numbers = ["a" => 10, "b" => 20, "c" => 30, "d" => 40]; ["a" => $a, "c" => $c] = $numbers; echo $a."\n"; // 10 echo $c."\n"; // 30
In foreach loop
We can use the list function within a foreach
loop to unpack the array values. This is an easy way to only unpack those array values that we need during the execution of foreach
loop.
$students = [ ["roll_num" => 001, "name" => "John", "class" => "1st"], ["roll_num" => 002, "name" => "Sam", "class" => "2nd"] ]; // with list() foreach ($students as list("roll_num" => $roll_number, "name" => $name,"class" => $class)) { echo $roll_number ."\n"; echo $name ."\n"; echo $class ."\n"; } // with shorthand [] foreach ($students as ["roll_num" => $roll_number, "name" => $name,"class" => $class]) { echo $roll_number ."\n"; echo $name ."\n"; echo $class ."\n"; }
The Extract Function
The extract() is an inbuilt function within PHP. It does not allow specifying variable names as it auto-generates the variable names that are the same as the keys of an array.
It mostly works with an associative array as it uses the key name for creating variable names. A numerically indexed array will only be used with EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID flags parameter. Flags are also helpful in many conditions such as managing invalid variable names, overwriting existing variables etc.extract() associative array
$students = ["roll_num" => 001, "name" => "John", "class" => "1st"]; extract($students); echo $roll_num ."\n"; // 001 echo $name ."\n"; // John echo $class ."\n"; // 1st
extract() numerically indexed array
$hobbies = ["cricket", "carrom", "chess"]; extract($hobbies, EXTR_PREFIX_ALL, "array_value"); echo $array_value_0 ."\n"; //cricket echo $array_value_1 ."\n"; // carrom echo $array_value_2 ."\n"; // carrom
Object Destructuring
In PHP, there is no object destructuring, but by converting an object into an associative array we can apply destructuring to it.
To convert an object into an array we simply typecast it to an array or use the get_object_vars() function of PHP.
Both methods return the properties of an object with their values in an associative array and any property with no assigned value is returned as NULL.
In the following example we can see how we can destructure object properties:
class Student{ public $roll_number; public $name; public $class; public $address; } $student = new Student(); $student->roll_number = "001"; $student->name = "John"; $student->class = "1st"; // with inbuilt function get_object_vars list("roll_number" => $roll_number, "name" => $name, "class" => $class, "address" => $address) = get_object_vars($student); echo $roll_number."\n"; //001 echo $name."\n"; // John echo $class."\n"; // 1st echo $address."\n"; // NULL // by type casting an object into array list("roll_number" => $roll_number, "name" => $name, "class" => $class, "address" => $address) = (array) $student; echo $roll_number."\n"; //001 echo $name."\n"; // John echo $class."\n"; // 1st echo $address."\n"; // NULL
If you liked this post, here are a few more that may interest you,