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