Destructuring Assignments in PHP7

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,

  1. List
  2. 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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$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
$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
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$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
$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
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// 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
// 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
// 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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$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
$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
$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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[ "class" = > $ class , "roll_num" = > $roll_number, "name" = > $name ] = $student;
echo $roll_number; // 002
echo $name; // Sam
echo $ class ; // 2nd
["class" => $class, "roll_num" => $roll_number, "name" => $name] = $student; echo $roll_number; // 002 echo $name; // Sam echo $class; // 2nd
["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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// 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
// 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
// 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
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
foreach loop.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$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" ;
}
$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"; }
$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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$students = [ "roll_num" = > 001 , "name" = > "John" , "class" = > "1st" ] ;
extract ( $students ) ;
echo $roll_num . "\n" ; // 001
echo $name . "\n" ; // John
echo $ class . "\n" ; // 1st
$students = ["roll_num" => 001, "name" => "John", "class" => "1st"]; extract($students); echo $roll_num ."\n"; // 001 echo $name ."\n"; // John echo $class ."\n"; // 1st
$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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$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
$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
$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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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
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
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,

Learn More about Encora

We are the software development company fiercely committed and uniquely equipped to enable companies to do what they can’t do now.

Learn More

Global Delivery

READ MORE

Careers

READ MORE

Industries

READ MORE

Related Insights

Enabling Transformation in Hospitality through Technology-Led Innovation

As the exclusive sponsor of the 2024 Hotel Visionary Awards, we support organizations leading ...

Read More

Key Insights from HLTH 2024: The Future of Patient-Centered Healthcare

Discover key insights from HLTH 2024 on digital health, AI in diagnostics, data interoperability, ...

Read More

Data-Driven Engineering: Transforming Operations and Products from Insight to Impact

Discover how data-driven engineering transforms operations and product development, enhancing team ...

Read More
Previous Previous
Next

Accelerate Your Path
to Market Leadership 

Encora logo

Santa Clara, CA

+1 669-236-2674

letstalk@encora.com

Innovation Acceleration

Speak With an Expert

Encora logo

Santa Clara, CA

+1 (480) 991 3635

letstalk@encora.com

Innovation Acceleration