Jump to content

Insert key=value pair in array after given key


Jessica

Recommended Posts

If I have an array like this, what makes the most sense for inserting a key value pair after a given key? This is how I came up with to do it, after looking at the manual I'm not sure if there's a function I'm missing, because this seems way too complicated.

<?php
function addKV($arr, $keyToFind, $addKey, $addValue){
    $keys = array_keys($arr);
    $spot = array_search($keyToFind, $keys);
    $chunks = array_chunk($arr, ($spot+1));
    array_unshift($chunks[1], array($addKey=>$addValue));
    $arr = call_User_Func_Array('array_Merge', $chunks);
    return $arr;
}

$myArr = array('id'=>1, 'name'=>'Name', 'created'=>time(), 'modified'=>time());
$myArr = addKV($myArr, 'name', 'foo', 'bar');
?>

$myArr now looks like

array('id'=>1, name=>'Name', 'foo'=>'bar', 'created'=>time(), 'modified'=>time());

array_splice lets you insert into the middle of an array (if you know the numeric offset it should go into).

 

Can I ask why you have to insert into the array? You know that for most purposes the arrangement of the keys/values makes no difference at all?

@the letter E,

 

You should have read the link you pointed to in the manual.

 

array_walk() does not allow elements to be added or removed as this extract from your link states

 

  Quote
Only the values of the array may potentially be changed; its structure cannot be altered' date=' i.e., the programmer cannot add, unset or reorder elements. If the callback does not respect this requirement, the behavior of this function is undefined, and unpredictable. [/quote']
  Quote

@the letter E,

 

You should have read the link you pointed to in the manual.

 

array_walk() does not allow elements to be added or removed as this extract from your link states

 

  Quote
Only the values of the array may potentially be changed; its structure cannot be altered' date=' i.e., the programmer cannot add, unset or reorder elements. If the callback does not respect this requirement, the behavior of this function is undefined, and unpredictable. [/quote']

 

So true. Thanks for the reminder.

  Quote

Can I ask why you have to insert into the array? You know that for most purposes the arrangement of the keys/values makes no difference at all?

Yep. I'm doing a foreach on the keys as columns in an html table. They're in the order I want them to output. So my options were to either create another array with the correct order (in which case I still have to put the value for the column title in the right spot) or just keep the keys in the right order when the new value was added.

 

It looks like array_splice would replace the existing values in that spot, not insert a new one.

  Quote

If you look at the last example in example #1, they seem to have inserted a value with array_splice by using an offset of zero.

I just saw that one, I get it now. That's perfect, saves me several lines. Thanks!!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.