(PHP 5 >= 5.4)
array_insert — Insert an array into another array before/after a given key.
array array_insert( array $input, mixed $insert, mixed $key [, string $pos = 'after' ] )Merge the elements of the $array array after, or before, the designated $key from the $input array. It returns the resulting array.
$array— The input array.$insert— The value to merge.$key— The key from the$inputto merge$insertnext to.$pos— Wether to splice$insertbefore or after the$key. Can be either "before" or "after" (the default).
Returns the resulting array.
If $key is not one of the accepted types E_USER_ERROR will be thrown and NULL returned.
Example #1 array_insert() example
$arr1 = [
"name" => [
"type" => "string",
"maxlength" => "30",
],
"email" => [
"type" => "email",
"maxlength" => "150",
],
];
$ins1 = [
"phone" => [
"type" => "string",
"format" => "phone",
],
];
array_insert( $arr1, $ins1, "email" );
$arr2 = ["one", "two", "three"];
array_insert( $arr2, "one-half", 1, "before" );The above example will output:
Array(
'name' => Array(
'type' => 'string',
'maxlength' => '30',
),
'email' => Array(
'type' => 'email',
'maxlength' => '150',
),
'phone' => Array(
'type' => 'string',
'format' => 'phone',
),
)
Array(
0 => 'one',
1 => 'one-half',
2 => 'two',
3 => 'three',
)
$ composer require mcaskill/php-array-insert
Why are you not using composer? Download Function.Array-Insert.php from the gist and save the file into your project path somewhere.