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; } }
The array_merge() is a builtin function in PHP and is used to merge two or more arrays into a single array. This function is used to merge the elements or values of two or more arrays together into a single array.
in_array() function is utilized to determine if specific value exists in an array. It works fine for one dimensional numeric and associative arrays.
You can use the PHP array_unique() function and PHP array_merge() function together to merge two arrays into one array without duplicate values in PHP.
array_combine($keys, $values)
PS: Click on my answer! Its also a link!
you need array_combine.
<?php $a = array('green', 'red', 'yellow'); $b = array('avocado', 'apple', 'banana'); $c = array_combine($a, $b); print_r($c); ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With