<?php
if (!function_exists('insert_into_array_after')) {
/**
* Insert a given array into a target array after a specified key in the target array.
* If the key does not exist in the target array, the given array will simply be appended.
*
* @param (array) $targetArray | (string) $targetKey | (array) $newArray
* @return (array)
* @author Mark Townsend <mtownsend5512@gmail.com>
*/
function insert_into_array_after($targetArray, $targetKey, $newArray)
{
if (! array_key_exists($targetKey, $targetArray)) {
return array_merge($targetArray, $newArray);
}
$targetKeyPosition = array_search($targetKey, array_keys($targetArray)) + 1;
$result = array_merge(array_slice($targetArray, 0, $targetKeyPosition, true), $newArray, array_slice($targetArray, $targetKeyPosition, count($targetArray), true));
return $result;
}
}