Да, возможно, было время, когда я предпочитал использовать что-то подобное.Посмотрите на функцию ниже, это будет делать то, что вы хотите:
/** Easily append anywhere in associative arrays
* @param array $arr Array to insert new values to
* @param string|int $index Index to insert new values before or after
* @param array $value New values to insert into the array
* @param boolean $afterKey Insert new values after the $index key
* @param boolean $appendOnFail If key is not present, append $value to tail of the array
* @return array
*/
function arrayInsert($arr, $index, $value, $afterKey = true, $appendOnFail = false) {
if(!isset($arr[$index])) {
if($appendOnFail) {
return $arr + $value;
} else {
echo "arrayInsert warning: index `{$index}` does not exist in array.";
return $arr;
}
} else {
$index = array_search($index, array_keys($arr)) + intval($afterKey);
$head = array_splice($arr, $index);
return $arr + $value + $head;
}
}
Пример результата:
>>> $test = ['name'=>[], 'size'=>[], 'error'=>[], 'position'=>[], 'details'=>[]];
=> [
"name" => [],
"size" => [],
"error" => [],
"position" => [],
"details" => [],
]
>>> arrayInsert($test, 'size', ['details'=>$test['details']]);
=> [
"name" => [],
"size" => [],
"details" => [],
"error" => [],
"position" => [],
]