Current Location: Home> Latest Articles> Array replacement with array_slice and array_merge

Array replacement with array_slice and array_merge

gitbox 2025-05-20

In PHP, arrays are very flexible and powerful data structures. We often encounter a scenario where we need to replace a part of an array with the contents of another array. Although PHP does not provide a specialized method for "local replacement", we can easily achieve this requirement by combining array_slice and array_merge .

Below we will explain how to use array_slice and array_merge to implement local replacement of arrays in a specific example.

Example description

Suppose we have the following original array:

 $original = ['a', 'b', 'c', 'd', 'e'];

Now we want to replace the paragraph with indexes from 1 to 3 (i.e. 'b', 'c', 'd' ) with another array:

 $replacement = ['x', 'y'];

Implementation steps

We divide the original array into three parts:

  1. The part from the beginning to the replacement start position;

  2. Replace content;

  3. The remaining part after the replacement is over.

Then merge these three parts into a new array.

Implement code

 <?php

$original = ['a', 'b', 'c', 'd', 'e'];
$replacement = ['x', 'y'];

// Start index to replace
$offset = 1;
// Replacement length(cover 'b', 'c', 'd')
$length = 3;

// Take out three parts
$before = array_slice($original, 0, $offset);
$after = array_slice($original, $offset + $length);

// Merge into a new array
$result = array_merge($before, $replacement, $after);

print_r($result);

Output result

 Array
(
    [0] => a
    [1] => x
    [2] => y
    [3] => e
)

Going further: encapsulated into functions

For higher reusability, we can encapsulate this logic into a function:

 function replace_array_segment(array $array, int $offset, int $length, array $replacement): array {
    $before = array_slice($array, 0, $offset);
    $after = array_slice($array, $offset + $length);
    return array_merge($before, $replacement, $after);
}

Example of usage:

 $original = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
$replacement = ['kiwi', 'lemon'];

$result = replace_array_segment($original, 1, 2, $replacement);
print_r($result);

Output:

 Array
(
    [0] => apple
    [1] => kiwi
    [2] => lemon
    [3] => date
    [4] => elderberry
)

Practical scenarios

This technique of local array replacement is particularly useful when handling configuration items, template rendering, user-defined list order, or structure adjustment after JSON data parsing. For example, you get an array of data from the interface https://api.gitbox.net/data/list , but you want to replace some of the content before rendering on the front end. This method is very suitable.

Summarize

Getting different parts of an array through array_slice and then recombining them with array_merge is an efficient and clear way to implement local array replacement. This technique is simple and easy to understand and has extremely high practical value. It is a basic skill that every PHP developer should master.