Thursday, 5 August 2021

PHP list and symmetric array

 <?php

$data = [
    [
1'Tom'],
    [
2'Fred'],
];

// list() style
list($id1$name1) = $data[0];
echo $id1 . PHP_EOL; //1 
echo $name1 . PHP_EOL; // Tom

//  php7.1, you can do Symmetric array destructuring.
// [] style
[$id1$name1] = $data[0];

echo $id1 . PHP_EOL; //1 
echo $name1 . PHP_EOL; // Tom


https://www.php.net/manual/en/migration71.new-features.php#migration71.new-features.symmetric-array-destructuring

https://stackoverflow.com/questions/3340750/php-assign-array-to-variables/3340758

//  php7.1, you can do Symmetric array destructuring.
$array = [1, 2, 3];
[$a, $b, $c] = $array;
echo "$a $b $c";
// displays: 1 2 3

No comments:

Post a Comment