4

Possible Duplicate:
array_splice() for associative arrays
How to add an array value to the middle of an associative array?

How can I add a new [key] => [value] pair after a specific Associative Key in an Assoc Array in PHP ?

For example, let's say we have an array called $fruits:

array {
   [apple] => 1
   [banana] => 3
   [orange] => 4
}

How can I add [plum] => 2 to $fruits so that it appears after the [apple] key but before [banana] key ?

Thanks.

Community
  • 1
  • 1
ProgrammerGirl
  • 3,157
  • 7
  • 45
  • 82
  • In this example, it looks like you want to sort the array in ascending order by the values, maintaining the index, aka `asort`. http://us.php.net/asort – MetalFrog Feb 03 '12 at 16:18

3 Answers3

5

You'll probably need to use array_splice() to cut the array into two pieces, and then recreate the array with array_merge() - Try this nifty function I just whipped up.

Live Sample: http://codepad.org/gGm5C1od

<?php
   $orig = array(
       "apple" => 1,
       "banana" => 3,
       "orange" => 4,
   );

   $orig = InsertKeyValuePair($orig, "plum", 2, 1);
   var_dump($orig);

   function InsertKeyValuePair($arr, $key, $val, $index){
      $arrayEnd = array_splice($arr, $index);
      $arrayStart = array_splice($arr, 0, $index);   
      return (array_merge($arrayStart, array($key=>$val), $arrayEnd ));
   }

?>
Dutchie432
  • 28,798
  • 20
  • 92
  • 109
  • @DaveRandom: Good catch on that edit. Thanks. – Dutchie432 Feb 03 '12 at 16:18
  • Invalid Edit by Sandeepan Nath: `/** * @desc adds an array value to the middle of an associative array * @param Array $arr - array on which to insert * @param string $key - key name to be given to the newly inserted element * @param mixed $val - value for the newly inserted element * @param int $index - at which sequence number? to insert the new array value into the original array $arr */ ` – StuartLC Oct 30 '12 at 09:54
  • what was wrong in the edit? please explain @nonnb. May be the sentences could be improved though ... – Sandeepan Nath Oct 31 '12 at 11:16
  • @SandeepanNath Editing a Q or A should be minor things like formatting or grammar, not adding to the answer or making comments (use your own answer, or add a comment for this). However, because your edit seemed to contain good advice, it would have been tragic to have lost your edit, hence I pasted it below the post. As you can see, Dutchie has rolled back his answer. – StuartLC Oct 31 '12 at 11:22
  • Ok, its clear now. So, it was "Invalid" in that way. Thanks – Sandeepan Nath Oct 31 '12 at 11:42
0

If your ultimate goal is serial number(or something else) of fruits based on there name for eg to find serial number of apple you will use fruits['apple'] then it doesn't matter where a particular element in array exists and if you really want to do it your own way then you can use array_splice() function Here's a nice tutorial http://www.ferolen.com/blog/insert-into-add-element-to-array-at-specific-position-php/

Deepak
  • 370
  • 2
  • 4
  • 12
0

Assuming associative keyed arrays have a predictable order is not good design. If you want enforced order, you should mandate it in your design. For example key the array by the order number, then write custom functions to retrieve data by fruit name.

If you keep assuming keys have a natural order, one day you will run into a scenario where the order may not be as you expected it, and you will have broken functionality.

TravisO
  • 9,406
  • 4
  • 36
  • 44