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 Validation

PHP validation is used to check whether the field is filled or not in the proper way by the user. There are two types of validation in PHP. Client Side Validation:...

5 minutes read.

WordPress Theme

Writing our WordPress Theme is relatively simple. Requirements: Before we begin, we need to know about the following set of requirements. We need to have full knowledge of WordPress setup, which is either...

2 minutes read.

PHP strtr() Function

PHP strtr() Function The strtr() function in PHP translates or replaces certain characters in a string. Syntax strtr ( string $str , string $from , string $to ) or strtr ( string $str , array $replace_pairs ) Parameter str(required)- This parameter specifies the string to translate from(required)- This parameter represents the string being translated to the parameter ‘to’. to(required)- It specifies what...

1 minute read.

PHP settype() Function

PHP settype() Function The settype() function in PHP is used to set the type of variable var to type. Syntax settype ( mixed &$var , string $type ) Parameter var(required)- This parameter represents the variable being converted. type(required)- This parameter represents the type of variable that is needed.  The possible values of type are...

1 minute read.

PHP addcslashes() Function

The addcslashed() function in PHP is used to add quote string with slashes in a C style. It is case-sensitive function.  Syntax addcslashes (str, charlist ) Parameter str(required)- This parameter represents the string to be escaped. charlist(required)-...

1 minute read.

PHP String get_html_translation_table() Function

The get_html_translation_table() function in PHP is used to return the translation table used by the htmlentities() and htmlspecialchars() functions. Syntax get_html_translation_table ([ int $table = HTML_SPECIALCHARS [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = "UTF-8" ]]] ) Parameter table (optional)- This parameter specifies which translation table to return. It...

2 minutes read.

PHP array_pop() Function

PHP array_pop() Function The array_pop() function in PHP pops or removes the last element from the array shortening the array by one element. Syntax array_pop ( array &$array ) Parameter array(required)- This parameter represents the input array to get the value...

1 minute read.

PHP array_combine() Function

PHP array_combine() Function The array_combine() function in PHP is used to create an array by combining two arrays i.e., one array for keys and another array for its values. Syntax array_combine ( array $keys , array $values ) Parameter keys(required)- This parameter represents an array which acts...

1 minute read.

PHP array_search() Function

PHP array_search() Function The array_search() function in PHP searches the array for the specified value. This function returns the first corresponding key if the value is found. Syntax array_search ( mixed $needle , array $haystack [, bool $strict ])  Parameter needle(required)- This parameter represents the value to...

1 minute read.

PHP quotemeta() Function

PHP quotemeta() Function The quotemeta() function in PHP quotes the meta characters and returns a string with a backslash character (\) before some predefine characters. The predefined characters are as follows: period ...

1 minute read.

PHP substr_replace() Function

PHP substr_replace() Function The substr_replace() function in PHP replaces a part of a string with another string. Syntax substr_replace (mixed $string , mixed $replacement , mixed $start [, mixed $length ] ) Parameter string(required)- This parameter represents the input string. replacement(required)- This parameter signifies the string to replace with. start(required)-...

2 minutes read.

PHP stripos() Function

PHP stripos() Function The stripos() function in PHP finds the position of the first occurrence of a substring in the specified string. It is a case-insensitive a binary-safe function. The related functions are as follows: strripos() Functionstrpos() Functionstrrpos() Function Syntax stripos ( string $haystack , mixed $needle [, int $offset...

1 minute read.

PHP array_intersect_ukey() function

The array_intersect_ukey () function in PHP is used to compute the intersection of arrays with the help of a callable function on the keys for comparison. It returns an array containing all the...

1 minute read.

PHP array_push() Function

PHP array_push() Function The array_push() function in PHP pushes one or more elements onto the end of the array. This function increases the length of the array by the number of variables pushed. Syntax array_push ( array &$array, mixed& value [, mixed $... ]...

1 minute read.

PHP sscanf() Function

PHP sscanf() Function The sscanf() function in PHP parses input from a string according to a given format. Syntax sscanf ( string $str , string $format [,mixed &$arg1,$agr2,$arg++... ] ) Parameter str(required)- This parameter specifies the string to read. format(required)- This parameter specifies the format to use. The conversion specification...

3 minutes read.

PHP array_map() Function

PHP array_map() Function The array_map() function  in PHP is used to return an array containing the results of applying the user-defined callback function over the elements of one or more arrays. Syntax array_map ( callable $callback , array $array1 [, array $... ] )  Parameter callable(required)- This parameter...

1 minute read.

PHP uasort() Function

PHP uasort() Function The uasort() function in PHP sorts an array with a user-defined comparison function and maintains the index association. Syntax uasort ( array &$array , callable $value_compare_func ) Parameter array(required)- This parameter represents the input array. value_compare_func(required)- This parameter represents a string that describe a...

1 minute read.

PHP strchr() Function

PHP strchr() Function The strchr() function in PHP searches for the first occurrence of the specified string inside another string. It is a binary-safe case -sensitive function and is the alias of strstr() function. Syntax strstr ( string $haystack , mixed $needle [, bool $before_needle] ) Parameter haystack(required)-...

1 minute read.

PHP substr() Function

PHP substr() Function The substr() function in PHP returns the portion of the string specified by the start and length parameters. Syntax substr ( string $string , int $start [, int $length ] )  Parameter string (required)- This parameter represents the input string. It must be one character or longer. start (required)- This parameter specifies where to start...

2 minutes read.

PHP chop() Function

PHP chop() Function The chop() function in PHP removes whitespaces or other predefined characters from the right end of the specified string. Syntax chop(str,charlist) Parameter str(required)- This parameter specifies the string to check. charlist(optional)- This parameter...

1 minute read.