PHP array_replace() Function

PHP array_replace() Function

The array_replace() function in PHP replaces the elements from passed arrays into the first array with values having the same keys in each of the following arrays.

Syntax

array_replace ( array $array1 [, array $... ] )

Parameter

array(required)- This parameter represents the input array in which elements are replaced.

...(optional)- This parameter signifies one or more arrays from which elements will be extracted.

Return

This function either returns the replaced array or it returns NULL if an error occurs.

Example 1

 "e", 2 => "i");
 //Return an array after Replacing the elements as passed in replacements array
 $replaced_array=array_replace($array, $replacements); 
 echo("\nReplaced elements are:\n");
 print_r($replacements);
 echo("Replaced Array: \n");
 //printing the replaced array
 print_r($replaced_array);
 ?> 

Output

Actual Array: 
 Array
 (
     [0] => a
     [1] => b
     [2] => c
     [3] => o
     [4] => u 
 )
 Replaced elements are:
 Array
 (
     [1] => e
     [2] => i
 )
 Replaced Array:  
 Array
 (
     [0] => a
     [1] => e
     [2] => i
     [3] => o
     [4] => u 
 ) 

Example 2

 "pineapple", 4 => "cherry");
 $replacements2 = array(0 => "grape");
 //Return an array after Replacing the elements from replacements arrays 
 $replaced_array=array_replace($array, $replacements1,$replacements2);
 echo("Replaced Array: \n");
 //printing the replaced array
 print_r($replaced_array);
 ?> 

Output

Actual Array: 
 Array
 (
     [0] => apple
     [1] => banana
     [2] => guava
     [3] => orange
     [4] => strawberry
 )
 Replaced Array:  
 Array
 (
     [0] => grape
     [1] => banana
     [2] => guava
     [3] => orange
     [4] => cherry 
 ) 

Example 3

 "pineapple", 4 => "cherry");
 //Return an array after Replacing the elements from replacements arrays
 $replaced_array=array_replace($array, $replacements1); 
 echo("Replaced Array: \n");
 //printing the replaced array
 print_r($replaced_array);
 ?> 

Output

Actual Array: 
 Array
 (
     [0] => 
 )
 Replaced Array: 
 Array 
 (
     [0] => pineapple
     [4] => cherry
 ) 

Related Topics

PHP Associative Arrays

PHP associate array is used to access the elements with keys by “=>” symbol. There are two ways to create an associative array. 1st way: $age = array("John"=>"20", "Helena"=>"30", "Rasmus"=>"29"); 2nd way: $age['john'] = "20"; $age['Helena'] =...

1 minute read.

PHP list() Function

PHP list() Function The list() function in PHP sorts an array by key while maintaining the keys. Syntax list ( mixed $var1 [, mixed $... ] ) Parameter var1(required)- This parameter accepts variables separated by spaces. …(optional)- This parameter accepts a list of variables separated...

1 minute read.

PHP array_values() Function

PHP array_values() Function The array_values () function in PHP returns all the values from the array and indexes the array numerically. Syntax array_values ( array $array ) Parameter array(required)- This array represents the input array. Return This function returns an indexed array of values. Example 1 Output Input Array: Array ...

1 minute read.

PHP vsprintf() Function

PHP vsprintf() Function The vsprintf() function in PHP returns a formatted string. This function accepts an array of arguments. Syntax vsprintf ( string $format , array $args ) Parameter format(required)- This parameter specifies the string and how to format the variables in it. The conversion specification of this parameter...

3 minutes read.

PHP printf() Function

PHP printf() Function The printf() function in PHP outputs a formatted string. The related functions of printf() function are as follows: sprintf()vprintf()vsprintf()fprintf()vfprintf() Syntax printf ( string $format [, mixed $... ] )  Parameter format(required)- This parameter formats the string which is composed of zero or...

1 minute read.

PHP count_chars() Function

PHP count_chars() Function The count_chards() function in PHP is used to return information about characters in a string. Syntax count_chars ( string $string [, int $mode = 0 ] ) Parameter string(required)- This parameter represents the string to be checked. mode(optional)- It specifies the return modes Return This function returns one...

2 minutes read.

PHP array_key_last() function

The array_key_last () function in PHP gets the last key of an array if the specified array is not empty. Syntax array_key_last ( array $array )  Parameter array(required)- This parameter signifies the input array. Return This function returns the last key of the...

1 minute read.

PHP Example

We can create a simple program by writing the source code inside the php tag and save it with .php extension. Syntax: <?php // your code here ?> Let us take an example of PHP. <!DOCTYPE html> <html> <head>            ...

1 minute read.

PHP unset() Function

PHP unset() Function The unset() function in PHP is used to unset a given variable. This function destroys the specified variables. If this function is called inside any user-defined function, then it unsets the value associated with...

1 minute read.

PHP rtrim() Function

PHP rtrim() Function The rtrim() function in PHP is used to strip off the whitespace (or other characters) from the end of a string. If the second parameter is not specified, this function will strip are...

1 minute read.

Python if-else

Python if-else The else statement is associated with if statement. An else statement executes if the conditional expression in if the statement becomes false or a Zero value. If the conditional expression...

2 minutes read.

PHP prev() Function

PHP prev() Function The prev() function in PHP rewinds or moves back the internal array pointer one place backward and returns the prev array value. It behaves like next() function, except that this function rewinds the...

1 minute read.

PHP is_int() Function

The PHP is_int() function in PHP is used to  find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it...

1 minute read.

PHP echo() Function

PHP echo() Function The echo() function in PHP outputs or displays one or more strings. The major differences between print() function and echo() function is that echo accepts an argument list and doesn't have a return value. Also, it is slightly...

1 minute read.

PHP key_exists() Function

PHP key_exists() Function The key_exists() function in PHP checks if the specified key or index exists in the array or not. Syntax key_exists ( mixed $key , array $array )  Parameter key(required)- This parameter represents the value to check. array(required)- This parameter signifies an input array with...

1 minute read.

PHP is_null() Function

The is_null() function in PHP is used to  find whether the specified variable is a null or not. It returns a Boolean value true if the parameter var is null else it returns...

1 minute read.

PHP sha1() Function

PHP sha1() Function The sha1() in PHP calculates the SHA-1 hash of a string. Syntax sha1 ( string $str [, bool $raw_output = FALSE ] )  Parameter str (required)- This parameter represents the input string. raw_output(optional)- This parameter takes Boolean value and returns the digest in raw binary format...

1 minute read.

PHP parse_str() Function

PHP parse_str() Function The parse_str() function in PHP parses a query string into variables. Syntax parse_str ( string $encoded_string [, array &$result ] ) Parameter encoded_string(required)- This parameter represents the input string. result(optional)- This parameter specifies the name of an...

1 minute read.

PHP array_key_first() function

The array_key_first () function in PHP gets the first key of an array if the specified array is not empty. Syntax array_key_first(array;$array) Parameter array(required)- This parameter signifies the input array. Return This function returns the first key of the specified...

1 minute read.

PHP pos() Function

PHP pos() Function The pos() function in PHP returns the current element in an array which is initialized to the first element of the array. This function is an alias of current(). Syntax pos ( array...

1 minute read.