1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PHPでES6っぽくdestructuring of arrays

Posted at

PHP7.1で追加された destructuring of arrays は、下記のようだが

$user = ['name' => 'Nancy'];
['name' => $userName] = $user;

echo $userName // => Nancy

下記のようにネストできることに気づいた

$user = [
    "address" => [
        "country" => "jp",
        "prefecture" => "Hokkaido",
    ],
];

['address' => ['country' => $country, 'prefecture' => $prefecture]] = $user;

echo $country; // => "jp"
echo $prefecture; // => "Hokkaido"

ES6みたいに、キー名がそのまま変数名になったら良いのに・・・できる方法は無いのだろか。

1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?