note 119355 deleted from function.array-merge by salathe
| From: | salathe@php.net | Date: | Mon, 06 Sep 2021 22:46:49 +0000 |
| Subject: | note 119355 deleted from function.array-merge by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-216904@lists.php.net to get a copy of this message | ||
Note Submitter: jcmarchi at gmail dot comfive
----
It is not officially documented but it is summarily important information for everyone to know:
neither array_merge or array_merge_recursive functions will function correctly if non-array objects
are used as parameters.
You would probably expect these functions to ignore non-array elements, right? However, if one
parameter is null it PHP will not only return null for the entire function but will also (not
always) raise an error, such as :
[ Warning: array_merge(): Argument #x is not an array... ]
This error message won't appear if the defective variable is an empty array (an empty array is
still an array), but it will result in an undesirable incomplete Array.
There are several solutions for this problem by validating the Arrays before use them in these
functions, but the most efficient way is to enforce the element as an array directly in the function
itself. I.e.:
$merged = array_merge( (array)$first_array, (array)$second_array );