Array destructuring – php 7.1
Kiedyś pisałem dosyć dużo kodu w javascript (es6). Wykorzystywałem różne możliwość języka, których później brakowało mi w php. Jedną z nich było tzw. destructuring assignment.
Przykład w ES6
const user = [1, 'name']; const [id, name] = user; console.log(id); // 1 console.log(name); // name
W php 5.6 też dało się coś takiego zrobić, ale tylko z użyciem funkcji list(). Natomiast w 7.1 już to według mnie wygląda bardziej przyjemnie.
PHP 5.6
$user = [1, 'name']; list($id, $name) = $user; var_dump($id); // 1 var_dump($name); // name
PHP 7.1
$user = [1, 'name']; [$id, $name] = $user; var_dump($id); // 1 var_dump($name); // name
Pętle
W przypadku tablic asocjacyjnych prezentuje się to następująco:
$user = ['id' => 1, 'name' => 'user']; ['id' => $id, 'name' => $name] = $user; var_dump($id); // 1 var_dump($name); // name
Teraz rozważmy trochę szerszy przykład. Załóżmy, że mamy taką tablicę z danymi użytkowników.
$users = [ ['id' => 1, 'name' => 'user', 'email' => 'user@test.com'], ['id' => 2, 'name' => 'user2', 'email' => 'user2@test.com'], ['id' => 3, 'name' => 'user3', 'email' => 'user3@test.com'] ];
Chcemy teraz przetworzyć w pętli nazwę i email użytkownika. Można to zrobić w następujący sposób.
$users = [ ['id' => 1, 'name' => 'user', 'email' => 'user@test.com'], ['id' => 2, 'name' => 'user2', 'email' => 'user2@test.com'], ['id' => 3, 'name' => 'user3', 'email' => 'user3@test.com'] ]; foreach ($users as ['name' => $name, 'email' => $email]) { var_dump($name); // user, user2, user3 var_dump($email); // user@test.com, user2@test.com, user3@test.com }
Zagnieżdżenia
Dodatkowo możliwe jest również wydobywanie zagnieżdżonych elementów.
$user = [ 'id' => 1, 'name' => 2, 'address' => [ 'city' => 'Krakow', 'street' => 'Wielicka' ] ]; ['address' => ['city' => $city]] = $user; var_dump($city); // Krakow
Subscribe and master unit testing with my FREE eBook (+60 pages)! 🚀
In these times, the benefits of writing unit tests are huge. I think that most of the recently started projects contain unit tests. In enterprise applications with a lot of business logic, unit tests are the most important tests, because they are fast and can us instantly assure that our implementation is correct. However, I often see a problem with good tests in projects, though these tests’ benefits are only huge when you have good unit tests. So in this ebook, I share many tips on what to do to write good unit tests.
Dodaj komentarz