Skip to content

Instantly share code, notes, and snippets.

@al5dy
Created August 15, 2016 18:24
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 al5dy/fa56a33999c8d5df9c6d0864f1ff8acf to your computer and use it in GitHub Desktop.
Save al5dy/fa56a33999c8d5df9c6d0864f1ff8acf to your computer and use it in GitHub Desktop.
Добавление элемента в опреденное место в ассоциативном массиве php.
$array = array(
'zero' => '0',
'one' => '1',
'two' => '2',
'three' => '3',
);
$res = array_slice($array, 0, 3, true) +
array("my_key" => "my_value") +
array_slice($array, 3, count($array) - 1, true) ;
print_r($res);
/*
* Array
(
[zero] => 0
[one] => 1
[two] => 2
[my_key] => my_value
[three] => 3
)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment