note 16026 deleted from function.array-merge by sniper
| From: | sniper@php.net | Date: | Mon, 06 Oct 2003 22:56:05 +0000 |
| Subject: | note 16026 deleted from function.array-merge by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57922@lists.php.net to get a copy of this message | ||
Note Submitter: dave@ovumdesign.com
----
Here's an easy way to write functions that accept either a scalar argument or an array. This
relies on an undocumented behavior of array_merge(), such that if a non-array arguement is supplied,
it is automatically converted into an array. I'm using PHP 4.0.3pl1 from Debian Potato.
function scalar_or_array($mixed) {
foreach (array_merge(array(), $mixed) as $item) {
echo "$item";
}
}
scalar_or_array('a');
scalar_or_array(array('b', 'c'));
This code should print "abc".