From php7.1, you can do Symmetric array destructuring.
https://www.php.net/manual/en/migration71.new-features.php#migration71.new-features.symmetric-array-destructuring
Code: (Demo)
$array = [1, 2, 3];
[$a, $b, $c] = $array;
echo "$a $b $c";
// displays: 1 2 3
Without calling list()
.
This is the new modern approach when "swapping" element positions without declaring a temporary variable. See examples here and here and here and here.
For in-depth breakdown and examples, have a long look at this post: https://sebastiandedeyne.com/the-list-function-and-practical-uses-of-array-destructuring-in-php/
As for using explode()
with list()
or array destructuring, if you are not guaranteed a certain number of elements,
it is best practice to declare the 3rd parameter of explode()
to limit the number of generated elements. This will not force the production of so many elements; rather it will merely tell php to stop exploding when that number of elements is achieved.
[$firstVariable, $secondVariable] = explode(' ', $stringToBeHalved, 2);
If you aren't 100% assured that your exploded data will provide balanced data to the other side of the assignment operator, you can implement a maximum with the technique above and use something akin to array_replace()
to provide a minimum number of elements on the right side of the assignment operator.
Code: (Demo)
$strings = [
"1,2,3,4",
"1,2,3",
"1,2"
];
$minElements = 3;
$defElements = array_fill(0, $minElements, null);
foreach ($strings as $string) {
[$a, $b, $c] = array_replace($defElements, explode(',', $string, $minElements));
var_export($a);
echo ' _ ';
var_export($b);
echo ' _ ';
var_export($c);
echo "\n-----------------\n";
}
Output:
'1' _ '2' _ '3,4'
-----------------
'1' _ '2' _ '3'
-----------------
'1' _ '2' _ NULL
-----------------
In fact, symmetric array destructuring even permits accessing the same element/value more than once -- which may feel a little awkward at first glance.
For example, you can push a solitary value into two arrays like this:
[0 => $array1[], 0 => $array2[]] = ['zeroIndexedElement'];
Read more at: While destructuring an array, can the same element value be accessed more than once?
And one more technique that is very seldom advised is to call extract()
. This function can be dangerous if you don't have complete control of the data that it is processing because it will push all of the data's keys into the global scope as variable names -- potentially overwriting variables and leading to script vulnerability or breakage.
To prepare the data for extraction, convert the indexed array into an associative array by assigning keys.
Code: (Demo)
$threeKeys = ['a', 'b', 'c'];
foreach ($strings as $string) {
extract(array_combine($threeKeys, explode(',', $string, 3)));
// now you can use $a, $b, and $c
}
The above demo shows some of the Warnings generated when not providing balanced/expected volumes of data. So the moral of this story is to be careful and eliminate possible fringe cases by ensuring the data processors will always receive what they require.