Jump to content

Insert key=value pair in array after given key


Jessica
 Share

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());

Link to comment
Share on other sites

@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

 

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']
Link to comment
Share on other sites

@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

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!!!

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.