はじめに
名前が似ていて混乱するので整理用に
array_splice
配列に対し要素を追加/置換/削除する
array_splice(array &$array, int $offset [, ?int $length[, mixed $replacement]]) :array
&$array : 操作対象の配列
$offset : 要素の抽出開始位置
$length : 取り出す要素数
$replacement : 削除箇所に挿入する配列(単一の場合は文字列でも可)
array_splice.php
$data = ['高江', '青木', '片渕', '和田', '花田', '佐藤'];
print_r(array_splice($data, 2, 3, ['日尾', '掛谷', '薄井'])); //['片渕', '和田', '花田']
print_r($data); //['高江', '青木', '日尾', '掛谷', '薄井', '佐藤']
print_r(array_splice($data, -3, -2, ['長田', '杉山'])); //['掛谷']
print_r($data); //['高江', '青木', '日尾', '長田', '杉山', '薄井', '佐藤']
print_r(array_splice($data, 3)); //['長田', '杉山', '薄井', '佐藤']
print_r($data); //['高江', '青木', '日尾']
print_r(array_splice($data, 1, 0, ['山田', '矢吹'])); //[]
print_r($data); //['高江', '山田', '矢吹', '青木', '日尾']
array_slice
配列から特定の要素を取り出す(スライスする)
array_slice(array $array, int $offset [, ?int $length[, bool $preserve_keys = false]]) :array
$array : 任意の配列
$offset : 抽出開始位置
$length : 取り出す要素数
$preserve_keys : 取得した要素のキーを維持するか
array_splice.php
$data = ['高江', '青木', '片渕', '和田', '花田', '佐藤'];
print_r(array_slice($data, 2, 3)); //[0 => '片渕', 1 => '和田', 2 => '花田']
print_r(array_slice($data, 2, 3, true)); //[2 => '片渕', 3 => '和田', 4 => '花田']
print_r(array_slice($data, 4)); //[0 => '花田', 1 => '佐藤']
print_r(array_slice($data, -4, -3)); //[0 => '片渕']