1

i have an associative array in PHP.

$myarray = array(
                  "a"=>"News",
                  "b"=>"Articles",
                  "c"=>"images"
                );

I want to insert some values after "a" key. so that the structure of array becomes

$myarray = array(
           "a"=>"News",
           "j"=>"Latest News",  
           "k"=>"Sports News",
           "l"=>"Entertainment",  
           "b"=>"Articles",
           "c"=>"images"
           );

How can i get this functionality.

HardCode
  • 1,613
  • 4
  • 21
  • 42

3 Answers3

2

The function for this is array_splice, but you are going to have to do some work manually because it does not preserve keys. Let's make it do that:

function search_and_insert($input, $afterKey, $newItems) {
    $keys = array_keys($input);
    $insertPosition = array_search($afterKey, $keys);
    if ($insertPosition === false) {
        return false;
    }
    ++$insertPosition;

    $newKeys = array_keys($newItems);
    array_splice($keys, $insertPosition, 0, $newKeys);
    array_splice($input, $insertPosition, 0, $newItems);
    return array_combine($keys, $input);
}

The idea here is that you process keys and values separately, splicing once for each array, and afterwards use array_combine to get the end result. Another good idea would be to write a reusable array_splice_assoc function using the same technique and use that instead of having one to do this specific job only.

Usage:

$myarray = array("a"=>"News", "b"=>"Articles", "c"=>"images");
$newItems = array("j"=>"Latest News", "k"=>"Sports News", "l"=>"Entertainment");
$insertAfter = "a";

print_r(search_and_insert($myarray, "a", $newItems));

See it in action.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

use this to inserts a new key/value after a specific key in the array:

 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;
    }

Usage :

$array = array("a"=>"News", "b"=>"Articles", "c"=>"images");

array_insert_after("a", $array, "j", "Latest News");
Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
-1

Array keys aren't preserved when using array_splice

<?php
$myarray = array(
                  "a"=>"News",
                  "b"=>"Articles",
                  "c"=>"images"
                );

$newarray = array(
                "j"=>"Latest News",
                "k"=>"Sports News",
                "l"=>"Entertainment");

var_dump($myarray);

array_splice($myarray, 1, 0, $newarray);

var_dump($myarray);
?>

OUTPUT

array(3) {
  ["a"]=>
  string(4) "News"
  ["b"]=>
  string(8) "Articles"
  ["c"]=>
  string(6) "images"
}

array(6) {
  ["a"]=>
  string(4) "News"
  [0]=>
  string(11) "Latest News"
  [1]=>
  string(11) "Sports News"
  [2]=>
  string(13) "Entertainment"
  ["b"]=>
  string(8) "Articles"
  ["c"]=>
  string(6) "images"
}
Pradeep Sanjaya
  • 1,816
  • 1
  • 15
  • 23
  • Do you see any differences between this program's output and the desired output as stated in the question? – Jon Apr 11 '12 at 07:45
  • Array keys aren't preserved when using array_splice, otherwise an elegant solution which was my first idea too :) – ChrisR Apr 11 '12 at 07:49
  • 1
    [array_splice for associative arrays](http://stackoverflow.com/questions/1783089/array-splice-for-associative-arrays) – Pradeep Sanjaya Apr 11 '12 at 08:05