/** * A re-implementation of the array splice function. * * @param type $input * @param type $start * @param type $length * @param type $replacement * @return type */ function array_splic(&$input, $start, $length = 0, $replacement = null) { $keys = array_Keys($input); $values = array_Values($input); if ($replacement !== null) { $replacement = (array) $replacement; $rKeys = array_Keys($replacement); $rValues = array_Values($replacement); } // optional arguments if ($replacement !== null) { array_splice($keys, $start, $length, $rKeys); array_splice($values, $start, $length, $rValues); } else { array_splice($keys, $start, $length); array_splice($values, $start, $length); } $input = array_Combine($keys, $values); return $input; }
public static function array_insert(&$original = array(), $insert = null, $position = 'after', $key = null, $sens = true) { if (!empty($insert)) { $after = $position == 'after' ? true : false; $insert = (array) $insert; $assoc = array_keys($insert) !== range(0, count($insert) - 1); $keep_keys = is_string($insert) || !$assoc || $sens; if (!empty($key) && array_key_exists($key, $original)) { $start = self::key_offset($original, $key); $start = $after ? $start + 1 : $start; if (!$keep_keys) { array_Splice($original, $start, 0, $insert); } else { $keys = array_Keys($original); $values = array_Values($original); $insert = (array) $insert; $rKeys = array_Keys($insert); $rValues = array_Values($insert); array_Splice($keys, $start, 0, $rKeys); array_Splice($values, $start, 0, $rValues); $original = array_Combine($keys, $values); } } else { $original = $after ? array_merge($original, $insert) : array_merge($insert, $original); } } }