15

Given an array:

$a = array(
    'abc',
    123,
    'k1'=>'v1',
    'k2'=>'v2',
    78,
    'tt',
    'k3'=>'v3'
);

With its internal pointer on one of its elements, how do I insert an element after the current element? And how do I insert an element after a key-known element, say 'k1'?

Performance Care~

6 Answers 6

14

You could do it by splitting your array using array_keys and array_values, then splice them both, then combine them again.

$insertKey = 'k1';

$keys = array_keys($arr);
$vals = array_values($arr);

$insertAfter = array_search($insertKey, $keys) + 1;

$keys2 = array_splice($keys, $insertAfter);
$vals2 = array_splice($vals, $insertAfter);

$keys[] = "myNewKey";
$vals[] = "myNewValue";

$newArray = array_merge(array_combine($keys, $vals), array_combine($keys2, $vals2));
1
  • There is a one-off error in this solution. It can be corrected by adding $insertAfter++; before the array_splice() calls.
    – keithm
    Mar 13, 2013 at 19:58
6

I found a great answer here that works really well. I want to document it, so others on SO can find it easily:

/*
 * Inserts a new key/value before the key in the array.
 *
 * @param $key
 *   The key to insert before.
 * @param $array
 *   An array to insert in to.
 * @param $new_key
 *   The key to insert.
 * @param $new_value
 *   An value to insert.
 *
 * @return
 *   The new array if the key exists, FALSE otherwise.
 *
 * @see array_insert_after()
 */
function array_insert_before($key, array &$array, $new_key, $new_value) {
  if (array_key_exists($key, $array)) {
    $new = array();
    foreach ($array as $k => $value) {
      if ($k === $key) {
        $new[$new_key] = $new_value;
      }
      $new[$k] = $value;
    }
    return $new;
  }
  return FALSE;
}

/*
 * Inserts a new key/value after the key in the array.
 *
 * @param $key
 *   The key to insert after.
 * @param $array
 *   An array to insert in to.
 * @param $new_key
 *   The key to insert.
 * @param $new_value
 *   An value to insert.
 *
 * @return
 *   The new array if the key exists, FALSE otherwise.
 *
 * @see array_insert_before()
 */
function array_insert_after($key, array &$array, $new_key, $new_value) {
  if (array_key_exists ($key, $array)) {
    $new = array();
    foreach ($array as $k => $value) {
      $new[$k] = $value;
      if ($k === $key) {
        $new[$new_key] = $new_value;
      }
    }
    return $new;
  }
  return FALSE;
}
1
  • Useful code, I just modified it to add value in case key does not exist. Aug 22, 2018 at 12:24
1

You can't use internal array pointer to insert elements.

There's array_splice which can insert/remove/replace elements and subarrays, but it's intended for integer-indexed arrays.

I'm afraid you'll have to rebuild the array to insert element (except cases where you want to insert first/last element) or use separate integer-indexed array for holding keys in the order you want.

1

Generally speaking doubly linked list would be ideal for this task.

There is a built-in implementation of that since PHP 5.3, called SplDoublyLinkedList and since PHP 5.5 it also has add method, which allows adding/inserting values in the middle.

2
0

This way is fine for new values without keys. You can not insert value with a key, and numeric indexes will be 'reset' as 0 to N-1.

$keys = array_keys($a);
$index = array_flip($keys);

$key = key($a); //current element
//or 
$key = 'k1';

array_splice($a, $index[$key] + 1, 0, array('value'));
0

There's a nice function that would help you here: https://gist.github.com/scribu/588429

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.