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

PHP get_defined_vars() Function The get_defined_vars() function in PHP returns an array of all defined variables. It returns a multidimensional array containing a list of all defined variables. Syntax get_defined_vars ( void ) Parameter NA Return This function returns a multidimensional array containing a list...

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

PHP quoted_printable_encode() Function The quoted_printable_encode() function in PHP converts a 8 bit string to a quoted-printable string. This function is similar to imap_8bit(), except this one does not require the IMAP module to work. Syntax quoted_printable_encode ( string $str ) Parameter str(required)- This parameter represents...

1 minute read.

PHP array_intersect_key() function

The array_intersect_key () function in PHP is used to compute the intersection of arrays using keys for comparison. It returns an array containing all the entries from array1 which have keys that are present in...

1 minute read.

PHP sort() Function

PHP sort() Function The sort() function in PHP sorts an array where the elements will be arranged from lowest to highest. Syntax sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )  Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to modify the...

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 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 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.

PHP MySQL Database Tutorial

MySQL is the most popular database system that is used within PHP. Let's first understand about MySQL. What is MySQL? MySQL is open-source Relational Database Management System (RDMS). The name of MySQL is...

13 minutes read.

Multiple-Inheritance in PHP

Multiple-Inheritance in PHP Multiple-Inheritance is the resources of the Object Oriented Programming Languages in which child class or subclass can inherit the resources of the multiple parent classes or superclasses. PHP does...

3 minutes read.

PHP Multidimensional Array

An array of arrays is called multidimensional array. It is used to store the data in a tabular form. It is represented in the form of matrix (row*column). Example: We can store...

2 minutes read.

PHP range() Function

PHP range() Function The range () function in PHP is used to create an array containing a range of elements. Syntax range ( mixed $start , mixed $end [, number $step = 1 ] ) Parameter start(required)- This parameter represents the first value of the sequence. end(required)- This parameter represents the...

1 minute read.

PHP convert_uudecode() Function

PHP convert_uudecode() Function The convert_uudecode() function in PHP decodes a uuencoded string. Syntax convert_uudecode ( string $data ) Parameter data- This parameter represents the uuencoded data. Return This function returns the decoded data as a string or FALSE on failure. Example 1 <?php // initializing the string $str...

1 minute read.

PHP is_integer() Function

The PHP is_integer() 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 returns...

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 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 Functions

PHP functions are used to reuse piece of code many times.  There are various built-in functions in PHP. A function will be executed by a call to the function. Following are...

3 minutes read.

PHP Variables

PHP Variable is the name of memory location used to hold the data.  In PHP, variable starts with $ symbol. Rules for PHP Variables. Variable starts with $ sign. Variable name...

2 minutes read.

PHP Date and Time

DATE: The PHP date() function formats a date or time. The timestamp is used to format a more readable date and time into a PHP date () function. Syntax: date(format,timestamp) Simple Date: Characters that...

1 minute read.

PHP array_pad() Function

PHP array_pad() Function The array_pad() function in PHP pads the array to the given length and value and returns a copy of the array padded to size and value specified by size and value parameter. The padding takes place as...

1 minute read.