views:

1090

answers:

3

Hi,

pretty straightforward question actually..

is it possible in PHP to combine two separate arrays of the same length to one associative array where the values of the first array are used as keys in the associative array?

I could ofcourse do this, but I'm looking for another (built-in) function, or more efficient solution..?

function Combine($array1, $array2) {
    if(count($array1) == count($array2)) {
        $assArray = array();
        for($i=0;$i<count($array1);$i++) {
            $assArray[$array1[$i]] = $array2[$i];
        }
        return $assArray;
    }
}
+3  A: 

array_combine($keys, $values)

PS: Click on my answer! Its also a link!

Chacha102
+2  A: 

There’s already an array_combine function:

$combined = array_combine($keys, $values);
Gumbo
+3  A: 

you need array_combine.

<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>
OneOfOne