PHP - Merge two arrays (same-length) into one associative?

php merge multidimensional array
php merge array values with same keys
php merge multidimensional array by key value
php array same key different value
php array merge unique
php array union
array_combine multidimensional php
addition of two array in php

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;
    }
}

array_combine($keys, $values)

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

Creates an array by using the values from the keys array as keys and the array_merge() - Merge one or more arrays; array_walk() - Apply a user supplied function to I needed to read CSV files into associative arrays with column headers as a row because array_combine returns false if both arrays don't have the same  Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not

you need array_combine.

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

print_r($c);
?>

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If an array key exists in both arrays, then the element from the first array will be used and $x=200; //size per array The concatenation of an associative array is also consistent with trying to merge the hash maps. Today in this tutorial we are going to merge two different arrays into single associative array in PHP. Last time i was creating travel based website and creating own engine in which the condition holiday package was to merge two different array of title an description into single array.

There’s already an array_combine function:

$combined = array_combine($keys, $values);

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  php merge 2 arrays into one associative array. Ask Question Asked 6 years, 5 months ago. php regex to extract multiple matches from string. Related. 4383.

hello everybody i will show you how to merge 2 arrays in one array

we have 2 arrays and i will make one array from them

 $data_key  = array('key1','key2');
 $data_value = array('val1','val2');

lets declare the main array

$main_array = array();

now let's fill it with the 2 arrays

foreach ($data_key as $i => $key) {
         $main_array[$key] = $data_value[$i];
}

now let's see the result by using var_dump($main_array);

array(2) { 
["key1"]=> string(4) "val1"
["key2"]=> string(4) "val2" 
}

i hope that can help someone :)

The array_merge() is a builtin function in PHP and is used to merge two or more arrays the elements or values of two or more arrays together into a single array. If two elements have the same string keys then the latter value will be overridden. PHP | Arrays · Multidimensional arrays in PHP · Associative Arrays in PHP  The array_merge() function merges one or more arrays into one array. Note: If two or more array elements have the same key, the last one overrides the others. Merge two or more arrays into one array in PHP

How to merge two or more arrays into one array in PHP You can use the PHP array_merge() function to merge the elements or values of two or Note: If the input arrays contain the same string keys, then the later value for that key will from an associative array in PHP · How to check if a key exists in an array in PHP. The array_merge () function merges one or more arrays into one array. Tip: You can assign one array to the function, or as many as you like. Note: If two or more array elements have the same key, the last one overrides the others. Note: If you assign only one array to the array_merge () function, and the keys are integers, the function returns

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  On PHP.net site, under array_merge, in Example #3 there's a comment: "If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator". And that turned to be true in my case.

While I was finding if there are some function that do that I found this on php.net basically is the same that you want but only for two args so. Flatten the data structure into an array of associative arrays or objects (conditionally) using old array functions ( current() and next() ), avoiding to look for the arrays max length. Then I ran into a problem when you have empty columns at the end of a row because array_combine returns false if both arrays don't have the same number of elements. This function based on quecoder at gmail's combine_arr() below allowed me to pad either array or not when parsing my CSVs to arrays.

Comments
  • "ass array", definitely an easy to recall name. ;)
  • but array combine dosn't take null values into a new array. :(
  • @gumuruh Could you elaborate on that?