Jessica Posted May 13, 2012 Share Posted May 13, 2012 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()); Quote Link to comment https://forums.phpfreaks.com/topic/262489-insert-keyvalue-pair-in-array-after-given-key/ Share on other sites More sharing options...
The Letter E Posted May 13, 2012 Share Posted May 13, 2012 I think you could simplify it using array_walk...maybe. http://php.net/manual/en/function.array-walk.php Quote Link to comment https://forums.phpfreaks.com/topic/262489-insert-keyvalue-pair-in-array-after-given-key/#findComment-1345195 Share on other sites More sharing options...
requinix Posted May 13, 2012 Share Posted May 13, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/262489-insert-keyvalue-pair-in-array-after-given-key/#findComment-1345196 Share on other sites More sharing options...
Barand Posted May 14, 2012 Share Posted May 14, 2012 @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'] Quote Link to comment https://forums.phpfreaks.com/topic/262489-insert-keyvalue-pair-in-array-after-given-key/#findComment-1345202 Share on other sites More sharing options...
The Letter E Posted May 14, 2012 Share Posted May 14, 2012 @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. Quote Link to comment https://forums.phpfreaks.com/topic/262489-insert-keyvalue-pair-in-array-after-given-key/#findComment-1345205 Share on other sites More sharing options...
Jessica Posted May 14, 2012 Author Share Posted May 14, 2012 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 Link to comment https://forums.phpfreaks.com/topic/262489-insert-keyvalue-pair-in-array-after-given-key/#findComment-1345313 Share on other sites More sharing options...
batwimp Posted May 14, 2012 Share Posted May 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262489-insert-keyvalue-pair-in-array-after-given-key/#findComment-1345324 Share on other sites More sharing options...
Jessica Posted May 14, 2012 Author Share Posted May 14, 2012 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/262489-insert-keyvalue-pair-in-array-after-given-key/#findComment-1345326 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.