Voting

: seven minus two?
(Example: nine)

The Note You're Voting On

loureirorg at gmail dot com
12 years ago
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
)

<< Back to user notes page

To Top