note 46780 added to function.array-combine
| From: | m71atfreedotfr at osu1 dot php dot net | Date: | Fri, 22 Oct 2004 22:39:49 +0000 |
| Subject: | note 46780 added to function.array-combine | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-78963@lists.php.net to get a copy of this message | ||
much shorter and more efficient:
<?php if (!function_exists('array_combine')){
function array_combine($a,$b) {
if (count($a)!=count($b)) return FALSE;
foreach($a as $key) list(,$c[$key])=each($b);
return $c;
}}
$a=array( 1=>"a",0=>NULL,NULL=>0,"b"); next($a);next($a);
$b=array( "b", 0, NULL,array()); next($b);
var_dump(array_combine($a,$b));
?>
yields:
array(4) { ["a"]=> string(1) "b" [""]=> int(0) [0]=> NULL
["b"]=> array(0) { } }
The weird keys in $a make that constructions using current() or next() would not work
(recall btw. that a NULL key is treated by PHP as empty string).
I added the 'next()' in the above to show that reset($b) is not needed.
Note that the above returns NULL if $a,$b are empty. If you prefer,
add "$c=array();" before "foreach" [to get a more logical result]
or "!count($a) or " after "if (" [to imitate the PHP5 function].
----
Manual Page -- http://www.php.net/manual/en/function.array-combine.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46780
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46780&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46780&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46780&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46780&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46780&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46780&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46780&report=yes
Search -- http://master.php.net/manage/user-notes.php