Skip to content

Instantly share code, notes, and snippets.

@mcaskill
Last active April 13, 2020 13:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcaskill/902ac7703021ce663eac to your computer and use it in GitHub Desktop.
Save mcaskill/902ac7703021ce663eac to your computer and use it in GitHub Desktop.
PHP : Insert elements from a passed array into the first array

array_insert

(PHP 5 >= 5.4)
array_insert — Insert an array into another array before/after a given key.

Description

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.

Parameters

  • $array — The input array.
  • $insert — The value to merge.
  • $key — The key from the $input to merge $insert next to.
  • $pos — Wether to splice $insert before or after the $key. Can be either "before" or "after" (the default).

Return Values

Returns the resulting array.

Errors/Exceptions

If $key is not one of the accepted types E_USER_ERROR will be thrown and NULL returned.

Examples

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',
)

Installation

With Composer

$ composer require mcaskill/php-array-insert

Without Composer

Why are you not using composer? Download Function.Array-Insert.php from the gist and save the file into your project path somewhere.

{
"name": "mcaskill/php-array-insert",
"description": "Insert elements from a passed array into the first array.",
"license": "MIT",
"authors": [
{
"name": "Chauncey McAskill",
"email": "chauncey@mcaskill.ca",
"homepage": "https://github.com/mcaskill"
}
],
"keywords": [
"function"
],
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"require": {
"php": ">=5.4.0"
},
"autoload": {
"files": ["Function.Array-Insert.php"]
}
}
<?php
if (!function_exists('array_insert')) {
/**
* Insert an array into another array before/after a certain key
*
* Merge the elements of the $array array after, or before, the designated $key from the $input array.
* It returns the resulting array.
*
* @param array $input The input array.
* @param mixed $insert The value to merge.
* @param mixed $key The key from the $input to merge $insert next to.
* @param string $pos Wether to splice $insert before or after the $key.
*
* @return array Returns the resulting array.
*/
function array_insert(array $input, $insert, $key, $pos = 'after')
{
if (!is_string($key) && !is_int($key)) {
trigger_error('array_insert(): The key should be a string or an integer', E_USER_ERROR);
}
$offset = array_search($key, array_keys($input));
if ('after' === $pos) {
$offset++;
}
if (false !== $offset) {
$result = array_slice($input, 0, $offset);
$result = array_merge($result, (array)$insert, array_slice($input, $offset));
}
else {
$result = array_merge($input, (array)$insert);
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment