note 119354 deleted from function.array-merge-recursive by cmb

From: Date: Fri, 20 May 2016 08:58:37 +0000
Subject: note 119354 deleted from function.array-merge-recursive by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-205518@lists.php.net to get a copy of this message
Note Submitter: jcmarchi at gmail dot com ---- 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 );

« previous php.notes (#205518) next »