Insert new item in array on any position in PHP
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How can I insert a new item into an array on any position, for example in the middle of array? 回答1: You may find this a little more intuitive. It only requires one function call to array_splice : $original = array( 'a', 'b', 'c', 'd', 'e' ); $inserted = array( 'x' ); // Not necessarily an array array_splice( $original, 3, 0, $inserted ); // splice in at position 3 // $original is now a b c x d e 回答2: A function that can insert at both integer and string positions: /** * @param array $array * @param int|string $position * @param mixed $insert