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 strtok() Function

PHP strtok() Function The strtok() function in PHP splits a string into smaller strings or tokenize the specified string.  Syntax strtok ( string $str , string $token ) Parameter str(required)- This parameter represents the input string to split into smaller strings or tokens. token(required)- It signifies...

1 minute read.

PHP strripos() Function

PHP strripos() Function The strripos() function in PHP finds the position of the last occurrence of a substring in the specified string. It is a case-insensitive and binary-safe function. Syntax strripos ( string $haystack , mixed $needle [, int $offset ] ) Parameter haystack(required)- This parameter signifies the...

1 minute read.

PHP is_float() Function

The PHP is_float() function in PHP is used to  find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter var is...

1 minute read.

PHP Continue

What is continue in PHP? Inside the loop, the continue statement is used to control the loop in preprocessor hypertext. PHP utilizes the continue keyword to implement the continue statement, which...

3 minutes read.

PHP Explode() Function

PHP Explode The PHP explode( ) is used to break a string into an array. The PHP explode( ) allow us to break a string into a smaller text with each break...

1 minute read.

PHP array_intersect_uassoc() function

The array_intersect_uassoc () function in PHP is used to compute the intersection of arrays with the help of an additional index check, comparing indexes by a callable function. It returns an array containing...

1 minute read.

PHP metaphone() Function

PHP metaphone() Function The metaphone() function in PHP calculates the metaphone key (representing how a string sounds or pronounced) of a string. This function is used to text search and text matching or spelling...

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 is_double() Function

The PHP is_double() function in PHP is used to  find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter var is...

1 minute read.

PHP Operator

PHP Operator is used to perform operations on variable and values. Following are the types of Operator: PHP Arithmetic Operator PHP Arithmetic operator is used for arithmetical operations, such as addition, subtraction, multiplication...

2 minutes read.

PHP array_change_key_case() Function

PHP array_change_key_case() Function The array_change_key_case() function in PHP is used to change the case of all keys present in the specified array. It returns an array with all keys in the specified array to lowercase or...

1 minute read.

PHP strnatcasecmp() Function

PHP strnatcasecmp() Function The strnatcasecmp() function in PHP compares two strings using a "natural" algorithm. It is a case-insensitive function. Syntax strnatcasecmp ( string $str1 , string $str2 )  Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less...

1 minute read.

PHP is_countable() Function

The PHP is_countable() function in PHP is used to  find whether a variable is countable or not. It returns a Boolean value true if the parameter var is countable else it returns false. Syntax is_countable ( mixed...

1 minute read.

PHP array_merge_recursive() Function

PHP array_merge_recursive() Function The array_merge_recursive() function  in PHP merges the elements of one or arrays together and returns the resulting array. Syntax array_merge_recursive (array $array  , [ array $... ] ) Parameter array(required)- This parameter specifies the input array ….(optional)- It represents more arrays to...

1 minute read.

PHP For loop with Example

For loop in PHP : The for loop is the most potent loop, it combines the abilities to set up variables as we enter the loop, test for conditions while iterating...

3 minutes read.

PHP array_splice() Function

PHP array_splice() Function The array_splice() function in PHP is used to remove a portion of the array and replaces them with the elements of the replacement array. Syntax array_splice (array &$input, int $offset [, int $length = count($input) [, mixed $replacement = array() ]] ) Parameter input(required)- This array represents the...

2 minutes read.

PHP boolval() Function

PHP boolval() Function The boolval() function in PHP is used to give the Boolean value for a given variable. Syntax boolval ( mixed $var ) Parameter var(required)- This parameter represents the value that is converted to a boolean. It can be a...

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 array_udiff_assoc() Function

PHP array_udiff_assoc() Function The array_udiff_assoc () function in PHP computes the difference between the specified arrays with additional index check and compares the data by a callback function. It returns all the values from array1 that are not...

1 minute read.

PHP number_format() Function

PHP number_format() Function The number_format() function in PHP formats a number with grouped thousands. This function accepts one, two, or four parameters (not three): Syntax number_format ( float $number [, int $decimals ] )             or number_format ( float $number , int $decimals  , string $dec_point, string$thousands_sep ) Parameter number(required)- This parameter represents the number to be formatted. If...

1 minute read.