@@ -132,6 +132,81 @@ function array_multisort(array &$array1, $array1_sort_order = SORT_ASC, $array1_
132
132
}
133
133
134
134
135
+ /**
136
+ * array_replace_recursive replaces the values of
137
+ * array1 with the same values from all the following
138
+ * arrays. If a key from the first array exists in the second array, its value
139
+ * will be replaced by the value from the second array. If the key exists in the
140
+ * second array, and not the first, it will be created in the first array.
141
+ * If a key only exists in the first array, it will be left as is.
142
+ * If several arrays are passed for replacement, they will be processed
143
+ * in order, the later array overwriting the previous values.
144
+ *
145
+ * array_replace_recursive is recursive : it will recurse into
146
+ * arrays and apply the same process to the inner value.
147
+ *
148
+ * When the value in the first array is scalar, it will be replaced
149
+ * by the value in the second array, may it be scalar or array.
150
+ * When the value in the first array and the second array
151
+ * are both arrays, array_replace_recursive will replace
152
+ * their respective value recursively.
153
+ *
154
+ * @param array $array1 The array in which elements are replaced.
155
+ * @param array $params Optional. Arrays from which elements will be extracted.
156
+ * @return array Returns an array, or NULL if an error occurs.
157
+ * @throws ArrayException
158
+ *
159
+ */
160
+ function array_replace_recursive (array $ array1 , array ...$ params ): array
161
+ {
162
+ error_clear_last ();
163
+ if ($ params !== []) {
164
+ $ result = \array_replace_recursive ($ array1 , ...$ params );
165
+ } else {
166
+ $ result = \array_replace_recursive ($ array1 );
167
+ }
168
+ if ($ result === null ) {
169
+ throw ArrayException::createFromPhpError ();
170
+ }
171
+ return $ result ;
172
+ }
173
+
174
+
175
+ /**
176
+ * array_replace replaces the values of
177
+ * array1 with values having the same keys in each of the following
178
+ * arrays. If a key from the first array exists in the second array, its value
179
+ * will be replaced by the value from the second array. If the key exists in the
180
+ * second array, and not the first, it will be created in the first array.
181
+ * If a key only exists in the first array, it will be left as is.
182
+ * If several arrays are passed for replacement, they will be processed
183
+ * in order, the later arrays overwriting the previous values.
184
+ *
185
+ * array_replace is not recursive : it will replace
186
+ * values in the first array by whatever type is in the second array.
187
+ *
188
+ * @param array $array1 The array in which elements are replaced.
189
+ * @param array $params Arrays from which elements will be extracted.
190
+ * Values from later arrays overwrite the previous values.
191
+ * @return array Returns an array, or NULL if an error occurs.
192
+ * @throws ArrayException
193
+ *
194
+ */
195
+ function array_replace (array $ array1 , array ...$ params ): array
196
+ {
197
+ error_clear_last ();
198
+ if ($ params !== []) {
199
+ $ result = \array_replace ($ array1 , ...$ params );
200
+ } else {
201
+ $ result = \array_replace ($ array1 );
202
+ }
203
+ if ($ result === null ) {
204
+ throw ArrayException::createFromPhpError ();
205
+ }
206
+ return $ result ;
207
+ }
208
+
209
+
135
210
/**
136
211
* Applies the user-defined callback function to each
137
212
* element of the array. This function will recurse
0 commit comments