php 배열 값 치환하기
array_replace
(PHP 5 >= 5.3.0, PHP 7)
array_replace — Replaces elements from passed arrays into the first array
Description ¶
array array_replace ( array $array1 , array $array2 [, array $... ] )
array_replace() replaces the values of array1 with values having the same keys in each of the following arrays. 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 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 several arrays are passed for replacement, they will be processed in order, the later arrays overwriting the previous values.
array_replace() is not recursive : it will replace values in the first array by whatever type is in the second array.
Parameters ¶
array1
The array in which elements are replaced.
array2
The array from which elements will be extracted.
...
More arrays from which elements will be extracted. Values from later arrays overwrite the previous values.
Return Values ¶
Returns an array, or NULL if an error occurs.
Examples ¶
Example #1 array_replace() example
<?php
$base = array("orange", "banana", "apple", "raspberry");
$replacements = array(0 => "pineapple", 4 => "cherry");
$replacements2 = array(0 => "grape");
$basket = array_replace($base, $replacements, $replacements2);
print_r($basket);
?>
The above example will output:
Array
(
[0] => grape
[1] => banana
[2] => apple
[3] => raspberry
[4] => cherry
)
'IT > php' 카테고리의 다른 글
array_diff 배열값 제외 출력 (0) | 2016.10.10 |
---|---|
array_flip 배열의 값과 첨자 바꾸기 (0) | 2016.10.10 |
php array_sum 배열의 값 합 구하기 (0) | 2016.10.06 |
php array_unique php 배열 중복값 제거 하기 (0) | 2016.10.06 |
php arsort 배열 첨자 기준 정렬 배열정렬 (0) | 2016.10.06 |