<?php
if (!function_exists('insert_into_array_before')) {
/**
* Insert a given array into a target array before 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_before($targetArray, $targetKey, $newArray)
{
if (! array_key_exists($targetKey, $targetArray)) {
return array_merge($targetArray, $newArray);
}
$targetKeyPosition = array_search($targetKey, array_keys($targetArray));
$result = array_merge(array_slice($targetArray, 0, $targetKeyPosition, true), $newArray, array_slice($targetArray, $targetKeyPosition, count($targetArray), true));
return $result;
}
}