note 101285 deleted from function.array-shift by danbrown
| From: | danbrown@php.net | Date: | Tue, 07 Dec 2010 18:30:56 +0000 |
| Subject: | note 101285 deleted from function.array-shift by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-174694@lists.php.net to get a copy of this message | ||
Note Submitter: news_yodpeirs at thoftware dot de
----
Just for fun - and for PHP4 ;-):
A function combining arrays using NULL as value if the value is missing (more keys than values) and
the next possible number as key if the key is missing (more values than keys).
As a special effect it also works with only one array, using the values of that array as keys and as
values.
Should work with any possible array. Keys are typecasted to strings before use. With duplicate keys
previous key-value-pairs will be overwritten.
function array_combine_4($keys,$vals=NULL) {
$comb = array();
if (!is_array($vals)) $vals = $keys;
foreach ($keys as $key) $out[(string)$key] = array_shift($vals);
while ($val = array_shift($vals)) $out[] = $val;
return($out);
}