Open In App

How to Replace an Element Inside Array in PHP ?

Last Updated : 11 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array containing some elements, the task is to replace an element inside the array in PHP. There are various methods to manipulate arrays, including replacing elements.

Examples:

Input: arr = [10, 20, 30, 40, 50], index_1 = 100
Output: [10, 100, 30, 40, 50]
Input: arr = ['GFG', 'Geeks', 'Hello'], index_2 = 'Welcome'
Output: ['GFG', 'Geeks', 'Welcome']

Here are some common approaches:

Approach 1: Using Index Assignment

The most straightforward way to replace an element in a PHP array is to directly assign a new value to the desired index.

PHP
<?php 

$arr = array(10, 30, 40, 50, 70); 

$arr[1] = 100; 

// Display the updated array 
print_r($arr); 

?>

Output
Array
(
    [0] => 10
    [1] => 100
    [2] => 40
    [3] => 50
    [4] => 70
)

Approach 2: Using array_replace() Function

PHP provides the array_replace() function that merges the values of two or more arrays. It is particularly useful for replacing specific elements.

PHP
<?php 

$arr = array(10, 30, 40, 50, 70); 

$modifiedArr = array_replace($arr, [1 => 100]); 

// Display the modified array 
print_r($modifiedArr); 

?>

Output
Array
(
    [0] => 10
    [1] => 100
    [2] => 40
    [3] => 50
    [4] => 70
)

Approach 3: Using array_splice() Function

The array_splice() function allows us to remove a portion of the array and replace it with new elements.

PHP
<?php 

$arr = array(10, 30, 40, 50, 70); 

array_splice($arr, 1, 1, 100); 

// Display the modified array 
print_r($arr); 

?>

Output
Array
(
    [0] => 10
    [1] => 100
    [2] => 40
    [3] => 50
    [4] => 70
)

Approach 4: Using foreach Loop with Condition

In PHP, a foreach loop with a condition iterates through an array. Inside the loop, a condition (`if` statement) checks each element (`$value`) against a specified condition (`$value === 3`), allowing for targeted updates or operations based on element values.

Example: In this example we iterates through an array, replaces the first occurrence of the value 3 with 6, then stops further iteration.

PHP
<?php
$array = [1, 2, 3, 4, 5];

foreach ($array as $key => $value) {
    if ($value === 3) {
        $array[$key] = 6;
        break; // Stop iterating once the condition is met
    }
}

print_r($array); // Output: [1, 2, 6, 4, 5]
?>

Output
Array
(
    [0] => 1
    [1] => 2
    [2] => 6
    [3] => 4
    [4] => 5
)

Approach 5: Using array_map() Function

The array_map() function applies a callback to the elements of the given arrays. It can be used to replace specific elements in an array by applying a custom function.

Example:

PHP
<?php 

$arr = array(10, 30, 40, 50, 70); 
$indexToReplace = 1; 
$newValue = 100; 

$modifiedArr = array_map(function($value, $key) use ($indexToReplace, $newValue) {
    return ($key === $indexToReplace) ? $newValue : $value;
}, $arr, array_keys($arr));

// Display the modified array 
print_r($modifiedArr); 

?>

Output
Array
(
    [0] => 10
    [1] => 100
    [2] => 40
    [3] => 50
    [4] => 70
)

Approach 5: Using array_search() and direct assignment

This approach uses array_search() to find the index of the element to replace and then directly assigns the new value to that index.

Example:

PHP
<?php
// Example array
$arr = [10, 20, 30, 40, 50];

// Find the index of the element to replace
$index = array_search(30, $arr);

if ($index !== false) {
    // Replace the element at the found index
    $arr[$index] = 100;
}

print_r($arr); // Output: [10, 20, 100, 40, 50]
?>

Output
Array
(
    [0] => 10
    [1] => 20
    [2] => 100
    [3] => 40
    [4] => 50
)

Next Article

Similar Reads