Hướng dẫn
Quảng cáo

Hàm array_replace() trong PHP

Hướng dẫn cách sử dụng hàm array_replace() về mảng trong lập trình PHP

Tác dụng của hàm array_replace()

The array_replace() function replaces the values in an array with the values from other arrays.

The following table summarizes the technical details of this function.

Return Value: Returns the replaced array, or NULL if an error occurs.
Version: PHP 5.3.0+

Syntax

The basic syntax of the array_replace() function is given with:

array_replace(array1, array2, ...)

The following example shows the array_replace() function in action.

<?php
// Sample arrays
$array1 = array("tea", "coffee", "chips");
$array2 = array("apple", "orange", "nuts");

// Replace the values of array1 with the values of array2
$result = array_replace($array1, $array2);
print_r($result);
?>

The array_replace() function replaces the values of the first array with the values from the following arrays in such a way that, if a key from the first array exists in the second array, its value will be replaced by the value from the second array. If the key exists in the second array, and not in the first, it will be created in the first array. If a key only exists in the first array, it will be left as is.

If multiple arrays are passed for the replacement, they will be processed in order, the later arrays overwriting the previous values. See more examples section for better understanding.


Parameters

The array_replace() function accepts the following parameters.

Parameter Description
array1 Required. Specifies the array in which elements are replaced.
array2 Optional. Specifies the array from which elements will be extracted.
... Optional. Specifies more array from which elements will be extracted. Values from later arrays overwrite the previous values.

More Examples

Here're some more examples showing how array_replace() function basically works:

The following example demonstrates what happens if a key exists in both first and second array, as well as if a key only exists in the first array and not in second array.

<?php
// Sample arrays
$array1 = array("a"=>"apple", "b"=>"ball", "c"=>"cat");
$array2 = array("a"=>"airplane", "b"=>"banana", "camel", "dog");

// Performing array replacement
$result = array_replace($array1, $array2);
print_r($result);
?>

The following example shows what happens if a key only exists in the second array not in first array.

<?php
// Sample arrays
$array1 = array("a"=>"apple", "b"=>"ball", "c"=>"cat");
$array2 = array("a"=>"airplane", "b"=>"bat", "c"=>"car", "e"=>"elephant");

// Performing array replacement
$result = array_replace($array1, $array2);
print_r($result);
?>

The following example shows what happens if a numeric key only exists in the second array.

<?php
// Sample arrays
$array1 = array("apple", "banana", "orange", "mango");
$array2 = array(0=>"pineapple", 2=>"grape", 4=>"kiwi");

// Performing array replacement
$result = array_replace($array1, $array2);
print_r($result);
?>

The following example shows what happens if several arrays are passed for replacement.

<?php
// Sample arrays
$array1 = array("apple", "banana", "orange", "mango");
$array2 = array(0=>"pineapple", 2=>"grape", 4=>"kiwi");
$array3 = array(0=>"watermelon", 1=>"papaya");

// Performing array replacement
$result = array_replace($array1, $array2, $array3);
print_r($result);
?>

Bài viết này đã giúp ích cho bạn?

Bài viết mới

Advertisements