Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 39 additions & 10 deletions reference/array/functions/array-replace.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@
<methodparam rep="repeat"><type>array</type><parameter>replacements</parameter></methodparam>
</methodsynopsis>
<para>
<function>array_replace</function> replaces the values of
<parameter>array</parameter> 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.
<function>array_replace</function> creates a new array and assigns items into
it for each key in each of the provided arrays. If a key appears in multiple
input arrays, the value from the right-most input array will be used.
</para>
<para>
<function>array_replace</function> is not recursive : it will replace
values in the first array by whatever type is in the second array.
<function>array_replace</function> does not process elements items recursively,
it replaces the entire value for each key when it does a replacement.
</para>
</refsect1>
<refsect1 role="parameters">
Expand Down Expand Up @@ -85,6 +80,40 @@ Array
[3] => raspberry
[4] => cherry
)
]]>
</screen>
</example>
<example>
<title>Example of how nested arrays are handled</title>
<programlisting role="php">
<![CDATA[
<?php
$base = [ 'citrus' => [ 'orange', 'lemon' ], 'pome' => [ 'apple' ] ];
$replacements = [ 'citrus' => [ 'grapefruit' ] ];
$replacements2 = [ 'citrus' => [ 'kumquat', 'citron' ], 'pome' => [ 'loquat' ] ];

$basket = array_replace($base, $replacements, $replacements2);
print_r($basket);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually prefer var_dump() then print_r() as it is whitespace sensitive in very awkward places :/

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just copied what the first example was doing. I've changed them both to var_dump().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did leave alone the array(...) syntax on the first example even though I used [ ... ] in the second.

?>
]]>
</programlisting>
&example.outputs;
<screen role="php">
<![CDATA[
Array
(
[citrus] => Array
(
[0] => kumquat
[1] => citron
)

[pome] => Array
(
[0] => loquat
)

)
]]>
</screen>
</example>
Expand Down