元の質問は少し不明確です。インデックス X を削除し、インデックス X の後のすべてのアイテムを配列の最初のアイテムとして配置することを理解しました。
$index2remove = 2;
$newArray1 = array_slice($foo, $index2remove+1); // Get items after the selected index
$newArray2 = array_slice($foo, 0, $index2remove); // get everything before the selected index
$newArray = array_merge($newArray1, $newArray2); // and combine them
または、より短く、メモリ消費量が少し少なくなります (ただし、読みにくくなります):
$index2remove = 2;
$newArray = array_merge(
array_slice($foo, $index2remove+1), // add last items first
array_slice($foo, 0, $index2remove) // add first items last
);
私のコードで値2を設定解除する必要はありません。単純にスライスしてください。これは、2 番目の splice 関数の -1 で行います。
$newArray = array_merge()
必要に応じて、に置き換えることができますが$foo = array_merge()
、元の配列を保存する必要がない場合は、2 番目にのみです。
編集:小さなエラーを変更しました、プレーンジェーンに感謝します