Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function array_insert(array $array, int $key, $value)
- {
- $length = count($array);
- $result = [];
- for ($i = 0; $i <= $length; ++$i) {
- if ($i === $key) {
- $result[$i] = $value;
- $result[$i + 1] = $array[$i];
- ++$i;
- }
- else if ($i > $key) {
- $result[$i] = $array[$i - 1];
- }
- else {
- $result[$i] = $array[$i];
- }
- }
- return $result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement