Skip to content

Instantly share code, notes, and snippets.

@azjezz
Created September 16, 2018 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azjezz/e174bb34a23d298fea4b9b25ff0ca8ca to your computer and use it in GitHub Desktop.
Save azjezz/e174bb34a23d298fea4b9b25ff0ca8ca to your computer and use it in GitHub Desktop.
<?php
$array = ['a' => 1 , 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6];
// get the “rest of the values” from an array, when doing destructuring assignment, as a new array.
[$a, $b, ...$others] = $array;
var_dump($a) // int(1)
var_dump($b) // int(2)
var_dump($others) // array('c' => 3, 'd' => 4, 'e' => 5, 'f' => 6)
// “re”-structure
$anotherArray = ['a' => $a, 'b' => $b, ...$others];
var_dump($anotherArray) // array('a' => 1 , 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6)
var_dump($anotherArray === $array) // bool(true)
$azjezz = [
'name' => 'azjezz',
'location' => 'tunisia',
];
$other = [
'name' => 'sameh',
];
// “last one in wins” – meaning if an array at the end of the list (on the right) has the same key as a previous array in the list, the previous value will be overwritten.
$sameh = [...$azjezz, ...$other];
var_dump($sameh) // array('name' => 'sameh', 'location' => 'tunisia')
$default = [
'alpha' => 10,
'beta' => 11,
];
$config = ['alpha' => 15];
$config = [...$default, ...$config];
var_dump($config); // array('alpha' => 15, 'beta' => 11);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment