note 119354 added to function.array-merge-recursive
| From: | jcmarchi at gmail dot com | Date: | Fri, 20 May 2016 04:05:10 +0000 |
| Subject: | note 119354 added to function.array-merge-recursive | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-205515@lists.php.net to get a copy of this message | ||
It is not officially documented but it is summarily important information for everyone to know:
neither array_merge_recursive or array_merge 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_recursive(): 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_recursive( (array)$first_array, (array)$second_array );
----
Server IP: 72.52.91.14
Probable Submitter: 2601:40a:8300:9100:78ef:258a:6289:cc28
----
Manual Page -- http://php.net/manual/en/function.array-merge-recursive.php
Edit -- https://master.php.net/note/edit/119354
Del: integrated -- https://master.php.net/note/delete/119354/integrated
Del: useless -- https://master.php.net/note/delete/119354/useless
Del: bad code -- https://master.php.net/note/delete/119354/bad+code
Del: spam -- https://master.php.net/note/delete/119354/spam
Del: non-english -- https://master.php.net/note/delete/119354/non-english
Del: in docs -- https://master.php.net/note/delete/119354/in+docs
Del: other reasons-- https://master.php.net/note/delete/119354
Reject -- https://master.php.net/note/reject/119354
Search -- https://master.php.net/manage/user-notes.php