Subscribe to PHP Freaks RSS

Frank de Jonge: Array destructuring in PHP

syndicated from www.phpdeveloper.org on May 1, 2018

Frank de Jonge has a post to his site sharing some helpful hints around the use of arrays in PHP. In this quick post he covers some of the array "superpowers", list assignments and nested destructuring.

One of the things I like the most about JavaScript, and PHP also to some extent, is how flexible and expressive they CAN be (but not always are). I also believe that JavaScript, PHP, and Python have a number of features that make them a good fit for serving the forefront of the web.

[...] The most versatile type of PHP, to me, is the array. The array can be used to act like many different classic data types. You can use them as a List, a Set (although that requires some specific handling), a HashMap, just to name a few.

He starts with the "superpowers" that arrays gained in PHP 7.1 (the square bracket syntax for destructuring) and some code examples showing it in action. He then moves on to show how this update works for list assignment. He finishes up the list with a look at nested destructuring and referencing values from subarrays with this same square bracket syntax.