How can we insert a new item into an array on any position, say for example in the middle of array?

or

How can we insert a new item into an array on any position?

php arrays insert

It only requires one function call to array_splice:

[pastacode lang=”php” manual=”%24original%20%3D%20array(%20%E2%80%98w’%2C%20%E2%80%98i’%2C%20%E2%80%98k’%2C%20%E2%80%98e’%2C%20%E2%80%98t’%20)%3B%0A%24inserted%20%3D%20array(%20’x’%20)%3B%20%2F%2F%20Not%20necessarily%20an%20array%0A%0Aarray_splice(%20%24original%2C%203%2C%200%2C%20%24inserted%20)%3B%20%2F%2F%20splice%20in%20at%20position%203%0A%2F%2F%20%24original%20is%20now%20w%20i%20k%20x%20e%20t%0A” message=”Php Code” highlight=”” provider=”manual”/]

Here is the solution for insert arrays:

[pastacode lang=”php” manual=”function%20array_insert(%26%24array%2C%20%24value%2C%20%24index)%0A%7B%0A%20%20%20%20return%20%24array%20%3D%20array_merge(array_splice(%24array%2C%20max(0%2C%20%24index%20-%201))%2C%20array(%24value)%2C%20%24array)%3B%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]

A function that can insert at both integer and string positions:

[pastacode lang=”php” manual=”%2F**%0A%20*%20%40param%20array%20%20%20%20%20%20%24array%0A%20*%20%40param%20int%7Cstring%20%24position%0A%20*%20%40param%20mixed%20%20%20%20%20%20%24insert%0A%20*%2F%0Afunction%20array_insert(%26%24array%2C%20%24position%2C%20%24insert)%0A%7B%0A%20%20%20%20if%20(is_int(%24position))%20%7B%0A%20%20%20%20%20%20%20%20array_splice(%24array%2C%20%24position%2C%200%2C%20%24insert)%3B%0A%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20%24pos%20%20%20%3D%20array_search(%24position%2C%20array_keys(%24array))%3B%0A%20%20%20%20%20%20%20%20%24array%20%3D%20array_merge(%0A%20%20%20%20%20%20%20%20%20%20%20%20array_slice(%24array%2C%200%2C%20%24pos)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%24insert%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20array_slice(%24array%2C%20%24pos)%0A%20%20%20%20%20%20%20%20)%3B%0A%20%20%20%20%7D%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]

Integer usage:

[pastacode lang=”php” manual=”%24arr%20%3D%20%5B%22one%22%2C%20%22two%22%2C%20%22three%22%5D%3B%0Aarray_insert(%0A%20%20%20%20%24arr%2C%0A%20%20%20%201%2C%0A%20%20%20%20%22one-half%22%0A)%3B%0A%2F%2F%20-%3E%0Aarray%20(%0A%20%200%20%3D%3E%20’one’%2C%0A%20%201%20%3D%3E%20’one-half’%2C%0A%20%202%20%3D%3E%20’two’%2C%0A%20%203%20%3D%3E%20’three’%2C%0A)%0A” message=”Php Code” highlight=”” provider=”manual”/]

String Usage:

[pastacode lang=”php” manual=”%24arr%20%3D%20%5B%0A%20%20%20%20%22name%22%20%20%3D%3E%20%5B%0A%20%20%20%20%20%20%20%20%22type%22%20%20%20%20%20%20%3D%3E%20%22string%22%2C%0A%20%20%20%20%20%20%20%20%22maxlength%22%20%3D%3E%20%2240%22%2C%0A%20%20%20%20%5D%2C%0A%20%20%20%20%22email%22%20%3D%3E%20%5B%0A%20%20%20%20%20%20%20%20%22type%22%20%20%20%20%20%20%3D%3E%20%22email%22%2C%0A%20%20%20%20%20%20%20%20%22maxlength%22%20%3D%3E%20%22160%22%2C%0A%20%20%20%20%5D%2C%0A%5D%3B%0A%0Aarray_insert(%0A%20%20%20%20%24arr%2C%0A%20%20%20%20%22email%22%2C%0A%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%22phone%22%20%3D%3E%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22type%22%20%20%20%3D%3E%20%22string%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22format%22%20%3D%3E%20%22phone%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%5D%0A)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/] [pastacode lang=”php” manual=”%2F%2F%20-%3E%0Aarray%20(%0A%20%20’name’%20%3D%3E%0A%20%20array%20(%0A%20%20%20%20’type’%20%3D%3E%20’string’%2C%0A%20%20%20%20’maxlength’%20%3D%3E%20’40’%2C%0A%20%20)%2C%0A%20%20’phone’%20%3D%3E%0A%20%20array%20(%0A%20%20%20%20’type’%20%3D%3E%20’string’%2C%0A%20%20%20%20’format’%20%3D%3E%20’phone’%2C%0A%20%20)%2C%0A%20%20’email’%20%3D%3E%0A%20%20array%20(%0A%20%20%20%20’type’%20%3D%3E%20’email’%2C%0A%20%20%20%20’maxlength’%20%3D%3E%20’160’%2C%0A%20%20)%2C%0A)%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

We can use this:

[pastacode lang=”php” manual=”foreach%20(%24array%20as%20%24key%20%3D%3E%20%24value)%20%0A%7B%0A%20%20%20%20if(%24key%3D%3D1)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%24new_array%5B%5D%3D%24other_array%3B%0A%20%20%20%20%7D%20%20%20%0A%20%20%20%20%24new_array%5B%5D%3D%24value%3B%20%20%20%20%0A%7D%0A” message=”Php Code” highlight=”” provider=”manual”/]

Here is the another Solution: –

PHP Code:

[pastacode lang=”php” manual=”%3C%3Fphp%20%20%0A%24original%20%3D%20array(%20’1’%2C’2’%2C’3’%2C’4’%2C’5’%20)%3B%20%20%0Aecho%20’Original%20array%20%3A%20′.%22%5Cn%22%3B%20%20%0Aforeach%20(%24original%20as%20%24x)%20%20%20%0A%7Becho%20%22%24x%20%22%3B%7D%20%20%0A%24inserted%20%3D%20’%24’%3B%20%20%0Aarray_splice(%20%24original%2C%203%2C%200%2C%20%24inserted%20)%3B%20%20%20%0Aecho%20%22%20%5Cn%20After%20inserting%20’%24’%20the%20array%20is%20%3A%20%22.%22%5Cn%22%3B%20%20%0Aforeach%20(%24original%20as%20%24x)%20%20%20%0A%7Becho%20%22%24x%20%22%3B%7D%20%20%0Aecho%20%22%5Cn%22%20%20%0A%3F%3E%20%0A” message=”Php Code” highlight=”” provider=”manual”/]

Sample Output:

Original array :
1 2 3 4 5
After inserting ‘$’ the array is :
1 2 3 $ 4 5

  • splice method can be used for adding and/or removing elements from an array.
  • The first argument specifies the location at which to begin adding or removing elements.
  • The second argument specifies the number of elements to delete.
  • When using splice to add elements to an array, the second argument would be zero.
  • The third and subsequent arguments are elements to be added to the array.
[pastacode lang=”php” manual=”var%20ar%20%3D%20%5B1%2C%202%2C%203%2C%204%2C%205%2C%206%5D%3B%0A%2F%2F%20arguments%3A%20start%20position%2C%20number%20of%20elements%20to%20delete%2C%20elements%20to%20add%0Aar.splice(3%2C%200%2C%20%E2%80%98d’%2C%20%E2%80%98e’%2C%20%E2%80%98f’)%3B%0A%0Aconsole.log(%20ar%20)%3B%20%2F%2F%20%5B1%2C%202%2C%203%2C%20%E2%80%9Cd%22%2C%20%E2%80%9Ce%22%2C%20%E2%80%9Cf%22%2C%204%2C%205%2C%206%5D%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

Hint for adding an element at the beginning of an array:

[pastacode lang=”php” manual=”%24a%20%3D%20array(‘first’%2C%20’second’)%3B%0A%24a%5B-1%5D%20%3D%20’i%20am%20the%20new%20first%20element’%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]

then:

[pastacode lang=”php” manual=”foreach(%24a%20as%20%24aelem)%0A%20%20%20%20echo%20%24a%20.%20’%20’%3B%0A%2F%2Freturns%20first%2C%20second%2C%20i%20am…%0A%0A” message=”Php Code” highlight=”” provider=”manual”/]

but:

[pastacode lang=”php” manual=”for%20(%24i%20%3D%20-1%3B%20%24i%20%3C%20count(%24a)-1%3B%20%24i%2B%2B)%0A%20%20%20%20%20echo%20%24a%20.%20’%20’%3B%0A%2F%2Freturns%20i%20am%20as%201st%20element%0A” message=”Php Code” highlight=”” provider=”manual”/]

  • Add Elements to the Beginning of an Array:
  • unshift method is used to add elements to the beginning of an array.
  • It accepts multiple arguments, adjusts the indexes of existing elements, and returns the new length of the array.
  • The unshift method modifies the array on which it is invoked.

First we invoke unshift passing a single argument, then multiple arguments, displaying the results using console.log:

[pastacode lang=”php” manual=”var%20ar%20%3D%20%5B’one’%2C%20’two’%2C%20’three’%5D%3B%0A%2F%2F%20add%20single%20element%0Aar.unshift(‘zero’)%3B%0Aconsole.log(%20ar%20)%3B%20%2F%2F%20%5B%22zero%22%2C%20%22one%22%2C%20%22two%22%2C%20%22three%22%5D%0A%0A%2F%2F%20add%20multiple%20elements%0Aar.unshift(0%2C%201%2C%202%2C%203)%3B%0Aconsole.log(%20ar%20)%3B%20%2F%2F%20%5B0%2C%201%2C%202%2C%203%2C%20%22zero%22%2C%20%22one%22%2C%20%22two%22%2C%20%22three%22%5D%0A” message=”Php Code” highlight=”” provider=”manual”/]

Here is simple function for insert new element after a specific key, while preserving integer keys:

[pastacode lang=”php” manual=”private%20function%20arrayInsertAfterKey(%24array%2C%20%24afterKey%2C%20%24key%2C%20%24value)%7B%0A%20%20%20%20%24pos%20%20%20%3D%20array_search(%24afterKey%2C%20array_keys(%24array))%3B%0A%0A%20%20%20%20return%20array_merge(%0A%20%20%20%20%20%20%20%20array_slice(%24array%2C%200%2C%20%24pos%2C%20%24preserve_keys%20%3D%20true)%2C%0A%20%20%20%20%20%20%20%20array(%24key%3D%3E%24value)%2C%0A%20%20%20%20%20%20%20%20array_slice(%24array%2C%20%24pos%2C%20%24preserve_keys%20%3D%20true)%0A%20%20%20%20)%3B%0A%7D%20%0A” message=”Php Code” highlight=”” provider=”manual”/]

Normally, with scalar values:

[pastacode lang=”php” manual=”%24elements%20%3D%20array(‘foo’%2C%20…)%3B%0Aarray_splice(%24array%2C%20%24position%2C%20%24length%2C%20%24elements)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/]

To insert a single array element into our array don’t forget to wrap the array in an array (as it was a scalar value!):

[pastacode lang=”php” manual=”%24element%20%3D%20array(‘key1’%3D%3E’value1’)%3B%0A%24elements%20%3D%20array(%24element)%3B%0Aarray_splice(%24array%2C%20%24position%2C%20%24length%2C%20%24elements)%3B%0A” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

Categorized in: