Skip to content

Instantly share code, notes, and snippets.

@pthurmond
Last active April 17, 2021 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pthurmond/5d5435782d90229ade5a9a0561421c00 to your computer and use it in GitHub Desktop.
Save pthurmond/5d5435782d90229ade5a9a0561421c00 to your computer and use it in GitHub Desktop.
This is a tool to allow a programmer to easily insert a new associative index into a desired position in a PHP associative array.
<?php
/**
* Insert an associative array into a specific position in an array.
*
* @param array $original
* The original array to add to.
* @param array $new
* The new array of values to insert into the original.
* @param int $offset
* The position in the array ( 0 index ) where the new array should go.
*
* @return array
* The new combined array.
*/
function array_splice_assoc(array $original, array $new, int $offset):array {
return array_slice($original, 0, $offset, TRUE)
+ $new
+ array_slice($original, $offset, NULL, TRUE);
}
/**
* Insert an associative array into a specific position in an array.
*
* @param array $original
* The original array to add to.
* @param string $key
* The array key to insert as.
* @param array $new
* The new array of values to insert into the original.
* @param int $offset
* The position in the array ( 0 index ) where the new array should go.
*
* @return array
* The new combined array.
*/
function array_splice_assoc_key(array $original, string $key, array $new, int $offset):array {
$new = [$key => $new];
return array_slice($original, 0, $offset, TRUE)
+ $new
+ array_slice($original, $offset, NULL, TRUE);
}
/**
* Example Usage...
*/
$external_array = [
'First' => ['something', 'another thing', 'tertiary thing'],
'Second Association' => [
'#type' => 'text',
'#title' => 'My really cool form array element',
'#value' => 435,
],
'Third Position' => [
'Person' => [
'name' => 'Bobby',
'phone' => '321-648-8888',
],
],
'Fourth thing' => 'Rando Value',
'5th Element' => [1, 2, 3, 5, 'I declare a thumb war'],
];
/*
* I want to insert a new associative index between 'Second Association' and
* 'Third Position'. There is no built-in PHP function to do this in one simple
* step. Requiring extra work. This is a shortcut.
*
* The array I want to insert is below.
*/
$my_new_array = [
'Mutants' => ['Wolverine', 'Jean Grey', 'The Beast'],
'Turtles' => ['Donatello', 'Raphael', 'Michelangelo', 'Leonardo'],
];
// You can either create the array with key yourself
$new_arr = [
'my_new_array' => $my_new_array
];
// Then insert
$result = array_splice_assoc($external_array, $new_arr, 2);
// Or skip the extra wrapping step above
$result2 = array_splice_assoc_key($external_array, 'my_new_array', $my_new_array, 2);
/**
* The result will look like this:
*
* $result = [
* 'First' => ['something', 'another thing', 'tertiary thing'],
* 'Second Association' => [
* '#type' => 'text',
* '#title' => 'My really cool form array element',
* '#value' => 435,
* ],
* 'my_new_array' => [
* 'Mutants' => ['Wolverine', 'Jean Grey', 'The Beast'],
* 'Turtles' => ['Donatello', 'Raphael', 'Michelangelo', 'Leonardo'],
* ],
* 'Third Position' => [
* 'Person' => [
* 'name' => 'Bobby',
* 'phone' => '321-648-8888',
* ],
* ],
* 'Fourth thing' => 'Rando Value',
* '5th Element' => [1, 2, 3, 5, 'I declare a thumb war'],
* ];
*
* This is really handy in Drupal form arrays and other arrays where positioning
* is important to the output.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment