PHP: How to Add an Array Element After a Specified Element?
It is often necessary to insert a new element into an existing array, but the new element needs to be added at a specific position, not at the beginning or end.
In PHP, there is no built-in function to add a value to the middle of an array. However, we can use the array_slice() function to split the array into two and add the new array element between them.
Contents:
Examples
Associative Array
Inserting by Index
Add an element after the specified index. In the example, it's after the second element:
$array = [ 'one' => 'one value', 'two' => 'two value', 'three' => 'three value', ]; $array = array_slice( $array, 0, 2 ) + [ 'newKey' => 'newValue' ] + $array; print_r( $array ); /* Array ( [one] => one value [two] => two value [newKey] => newValue [three] => three value ) */
Inserting after a specified key (by key name)
Specify the key after which a new element should be added:
$array = [ 'one' => 'one value', 'two' => 'two value', 'three' => 'three value', ]; $after_key = 'two'; $index = array_search( $after_key, array_keys( $array ) ); $array = array_slice( $array, 0, $index + 1 ) + [ 'newKey' => 'newValue' ] + $array; print_r( $array ); /* Array( [one] => one value [two] => two value [newKey] => newValue [three] => three value ) */
Indexed Array
$array = [ 'one', 'two', 'three', ]; $array = array_merge( array_slice( $array, 0, 2 ), [ 'newValue' ], array_slice( $array, 2 ) ); print_r( $array ); /* Array ( [0] => one [1] => two [2] => newValue [3] => three ) */
Universal Function
For this solution, I propose a universal function:
/**
* Inserts an array after a specified key in another array.
*
* This function searches for the specified key in the provided array. If the key is found,
* it inserts the given array immediately after that key. If the key is not found, the given
* array is appended to the end of the original array.
*
* @param array $array The original array to modify.
* @param mixed $key The key after which the new array should be inserted.
* @param array $insert_array The array to insert into the original array.
*
* @return void
*/
function array_insert_after_key( & $array, $key, $insert_array ) {
$index = array_search( $key, array_keys( $array ) );
// key is not found, add to the end of the array
if( $index === false ){
$array = array_merge( $array, $insert_array );
}
// split the array into two parts and insert a new element between them
else{
$array = array_merge(
array_slice( $array, 0, $index + 1, true ),
$insert_array,
array_slice( $array, $index + 1, null, true )
);
}
}
Usage Examples
Associative Array
Example 1:
$array = [ 'a' => 1, 'b' => 2, 'c' => 3 ]; array_insert_after_key( $array, 'b', [ 'newKey' => 'newValue' ] ); print_r( $array ); /* Array ( [a] => 1 [b] => 2 [newKey] => newValue [c] => 3 ) */
Example 2:
$array = [ 'a' => 1, 'b' => 2, 'c' => 3 ]; $inset_array = [ 'newKey' => 'newValue', 'newKey2' => 'newValue2', ]; array_insert_after_key( $array, 'b', $inset_array ); print_r( $array ); /* Array ( [a] => 1 [b] => 2 [newKey] => newValue [newKey2] => newValue2 [c] => 3 ) */
Indexed Array
Example 1:
$array1 = [ 1, 2, 3, ]; array_insert_after_key( $array1, 1, [ 'newValue' ] ); print_r( $array1 ); /* Array ( [0] => 1 [1] => 2 [2] => newValue [3] => 3 ) */
Example 2:
$array1 = [ 1, 2, 3, ]; array_insert_after_key( $array1, 1, [ 'newValue', 'newValue2' ] ); print_r( $array1 ); /* Array ( [0] => 1 [1] => 2 [2] => newValue [3] => newValue2 [4] => 3 ) */
--