note 111668 added to function.array-combine
| From: | loureirorg at gmail dot com | Date: | Thu, 14 Mar 2013 10:48:02 +0000 |
| Subject: | note 111668 added to function.array-combine | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-194070@lists.php.net to get a copy of this message | ||
If two keys are the same, the second one prevails.
Example:
<?php
print_r(array_combine(Array('a','a','b'), Array(1,2,3)));
?>
Returns:
Array
(
[a] => 2
[b] => 3
)
But if you need to keep all values, you can use the function below:
<?php
function array_combine_($keys, $values)
{
$result = array();
foreach ($keys as $i => $k) {
$result[$k][] = $values[$i];
}
array_walk($result, create_function('&$v', '$v = (count($v) == 1)?
array_pop($v): $v;'));
return $result;
}
print_r(array_combine_(Array('a','a','b'), Array(1,2,3)));
?>
Returns:
Array
(
[a] => Array
(
[0] => 1
[1] => 2
)
[b] => 3
)
----
Server IP: 69.147.83.199
Probable Submitter: 189.27.149.40
----
Manual Page -- http://php.net/manual/en/function.array-combine.php
Edit -- https://master.php.net/note/edit/111668
Del: integrated -- https://master.php.net/note/delete/111668/integrated
Del: useless -- https://master.php.net/note/delete/111668/useless
Del: bad code -- https://master.php.net/note/delete/111668/bad+code
Del: spam -- https://master.php.net/note/delete/111668/spam
Del: non-english -- https://master.php.net/note/delete/111668/non-english
Del: in docs -- https://master.php.net/note/delete/111668/in+docs
Del: other reasons-- https://master.php.net/note/delete/111668
Reject -- https://master.php.net/note/reject/111668
Search -- https://master.php.net/manage/user-notes.php