|
1 | 1 | <?xml version="1.0" encoding="utf-8"?> |
2 | 2 | <!-- $Revision$ --> |
3 | | -<!-- EN-Revision: fea4357a0ced6a776a2db2ef0d4c09c39ad41ae1 Maintainer: daijie Status: ready --> |
| 3 | +<!-- EN-Revision: 5cc10e8d90a9d0cbb35779b60580e772e7d660a1 Maintainer: daijie Status: ready --> |
| 4 | +<!-- CREDITS: mowangjuanzi --> |
4 | 5 | <refentry xml:id="function.array-replace" xmlns="http://docbook.org/ns/docbook"> |
5 | 6 | <refnamediv> |
6 | 7 | <refname>array_replace</refname> |
|
14 | 15 | <methodparam rep="repeat"><type>array</type><parameter>replacements</parameter></methodparam> |
15 | 16 | </methodsynopsis> |
16 | 17 | <para> |
17 | | - <function>array_replace</function> 函数使用后面数组元素相同 key 的值替换 <parameter>array</parameter> 数组的值。如果一个键存在于第一个数组同时也存在于第二个数组,它的值将被第二个数组中的值替换。如果一个键存在于第二个数组,但是不存在于第一个数组,则会在第一个数组中创建这个元素。如果一个键仅存在于第一个数组,它将保持不变。如果传递了多个替换数组,它们将被按顺序依次处理,后面的数组将覆盖之前的值。 |
| 18 | + <function>array_replace</function> 创建新数组,并为提供的数组中的每个 key |
| 19 | + 都分配元素。如果某个 key 出现在多个输入数组中,则将使用最右侧输入数组中的值。 |
18 | 20 | </para> |
19 | 21 | <para> |
20 | | - <function>array_replace</function> 是非递归的:它将第一个数组的值进行替换而不管第二个数组中是什么类型。 |
| 22 | + <function>array_replace</function> 不会递归处理元素项,它在替换时会替换每个 key 的值。 |
21 | 23 | </para> |
22 | 24 | </refsect1> |
23 | 25 | <refsect1 role="parameters"> |
@@ -63,21 +65,59 @@ $replacements = array(0 => "pineapple", 4 => "cherry"); |
63 | 65 | $replacements2 = array(0 => "grape"); |
64 | 66 |
|
65 | 67 | $basket = array_replace($base, $replacements, $replacements2); |
66 | | -print_r($basket); |
| 68 | +var_dump($basket); |
67 | 69 | ?> |
68 | 70 | ]]> |
69 | 71 | </programlisting> |
70 | 72 | &example.outputs; |
71 | 73 | <screen role="php"> |
72 | 74 | <![CDATA[ |
73 | | -Array |
74 | | -( |
75 | | - [0] => grape |
76 | | - [1] => banana |
77 | | - [2] => apple |
78 | | - [3] => raspberry |
79 | | - [4] => cherry |
80 | | -) |
| 75 | +array(5) { |
| 76 | + [0]=> |
| 77 | + string(5) "grape" |
| 78 | + [1]=> |
| 79 | + string(6) "banana" |
| 80 | + [2]=> |
| 81 | + string(5) "apple" |
| 82 | + [3]=> |
| 83 | + string(9) "raspberry" |
| 84 | + [4]=> |
| 85 | + string(6) "cherry" |
| 86 | +} |
| 87 | +]]> |
| 88 | + </screen> |
| 89 | + </example> |
| 90 | + <example> |
| 91 | + <title>嵌套数组的处理方式示例</title> |
| 92 | + <programlisting role="php"> |
| 93 | +<![CDATA[ |
| 94 | +<?php |
| 95 | +$base = [ 'citrus' => [ 'orange', 'lemon' ], 'pome' => [ 'apple' ] ]; |
| 96 | +$replacements = [ 'citrus' => [ 'grapefruit' ] ]; |
| 97 | +$replacements2 = [ 'citrus' => [ 'kumquat', 'citron' ], 'pome' => [ 'loquat' ] ]; |
| 98 | +
|
| 99 | +$basket = array_replace($base, $replacements, $replacements2); |
| 100 | +var_dump($basket); |
| 101 | +?> |
| 102 | +]]> |
| 103 | + </programlisting> |
| 104 | + &example.outputs; |
| 105 | + <screen role="php"> |
| 106 | +<![CDATA[ |
| 107 | +array(2) { |
| 108 | + ["citrus"]=> |
| 109 | + array(2) { |
| 110 | + [0]=> |
| 111 | + string(7) "kumquat" |
| 112 | + [1]=> |
| 113 | + string(6) "citron" |
| 114 | + } |
| 115 | + ["pome"]=> |
| 116 | + array(1) { |
| 117 | + [0]=> |
| 118 | + string(6) "loquat" |
| 119 | + } |
| 120 | +} |
81 | 121 | ]]> |
82 | 122 | </screen> |
83 | 123 | </example> |
|
0 commit comments