Demos

PHP Replace Array Value Example

One of the coolest things of array is that you can hold as much data as you need. So when dealing with arrays, there might have been a time when one would come across a requirement to replace or change array value with another value. In this article, I am going to show you how to use PHP to replace array valu by means of simple examples. Demos follow.

How To use PHP to Replace Array Value

Let’s consider a simple example. Let’s assume that we have an array that consists of fruit names. We will now choose a fruit name (value) and we will replace this value in this array with another value i.e. with another fruit name. In this scenario, there arises 2 cases.

1. Replacing the array value assuming that all values in the array are unique.

2. Replacing the array value assuming that some or all values in the array may be duplicate values of an original value.

So let’s start with the first case.

Example 1: Replacing the Array Value Using PHP assuming that all values in the array are unique

echo 'Example 1 - Replace value "apple" with value "strawberry"<br />';

//Declare an array
$my_fruits_array = array('apple', 'mango', 'grapes', 'pineapple', 'apple', 'guava', 'apple');

//Print the contents of the array.
echo 'Following are the original array contents:<br />';
echo '<pre>';
print_r($my_fruits_array);
echo '</pre>';

//Replace the value "apple" with "strawberry" in the array
$search_for = "apple";
$replace_with = "strawberry";
for($i=0;$i<count($my_fruits_array);$i++) 
{
	if($my_fruits_array[$i] == $search_for) 
	{
		$my_fruits_array[$i] = $replace_with;
	}
}

//Print the contents of the array.
echo 'Following are the array contents AFTER removing the duplicate values from array:<br />';
echo '<pre>';
print_r($my_fruits_array);
echo '</pre>';

Example 2: Replacing the Array Value Using PHP assuming that all mutiple values in the array need replacement

echo 'Example 2: Replace the value "apple" with value "strawberry" and "mango" value with "fig" value in array <br />';

//Declare an array
$my_fruits_array = array('apple', 'mango', 'grapes', 'pineapple', 'apple', 'guava', 'apple', 'mango');

//Print the contents of the array.
echo 'Following are the original array contents:<br />';
echo '<pre>';
print_r($my_fruits_array);
echo '</pre>';

//Replace the value "apple" with "strawberry" value and "mango" value with "fig" value in array
$replacements_array = array( 'apple' => 'strawberry', 'mango' => 'fig');

//For each of the replacement values in the array, loop through the array values
foreach ( $replacements_array as $key1 => $val1 )
{
	//Loop through each of the original $my_fruits_array so that we can check if the value matches with our replacement array key.If yes, we will replace the value of $my_fruits_array with the corresponding replacement value from $replacements_array array.
	foreach ( $my_fruits_array as $key2 => $val2 )
	{	
		//If the value to be reaplaced in replacements_array == already existing value in $my_fruits_array
		if( $key1 == $val2 )
		{
			//Then simply use the index of $my_fruits_array[ to update its value with the replacement value from $replacements_array
			$my_fruits_array[$key2] = $val1;
		}
	}
}

//Print the contents of the array.
echo 'Following are the array contents AFTER removing the duplicate values from array:<br />';
echo '<pre>';
print_r($my_fruits_array);
echo '</pre>';
That’s it!

Do you know of any other ways to use  PHP to replace array values? Feel free to suggest by commenting below.

Share your thoughts, comment below now!

*

*