PHP: merge two arrays while keeping keys instead of reindexing?

how can i merge two arrays (one with string => value pairs and another with int => value pairs) while keeping the string/int keys? none of them will ever overlap (because one has only strings and the other has only integers).

here is my current code (which doesn’t work, because array_merge is reindexing the array with integer keys):

// get all id vars by combining the static and dynamic
$staticIdentifications = array(
 Users::userID => "USERID",
 Users::username => "USERNAME"
);
// get the dynamic vars, formatted: varID => varName
$companyVarIdentifications = CompanyVars::getIdentificationVarsFriendly($_SESSION['companyID']);
// merge the static and dynamic vars (*** BUT KEEP THE INT INDICES ***)
$idVars = array_merge($staticIdentifications, $companyVarIdentifications);

thanks!

merge arrays without reindexing keys

I have two arrays that I want to merge recursively, so adding arrays is not an option. This is simple example without multilevels to demonstrate the problem: $a1 = Array( 5 => ‘pronoun’ ) $a2 = Arr

php – merge arrays keeping keys and incrementing

I’ve been trying to figure this one out all day but cant seem to explain, or get across what I’m trying to achieve. Lets say I have 2 arrays: Array ( [1] => Array ( [2] => Dashboard ) ) and Ar

Merge two arrays where keys match [duplicate]

This question already has an answer here: Elegant way to merge two arrays as key value pairs in PHP? 4 answers I have two arrays with matching keys and I need to merge the values of both into a

merge two arrays, while maintaining the numeric keys

im trying to merge two arrays together. both have numeric keys and are unique. when i use array_merge, it re-indexes starting at 0. so lets say i have [2] = abc [5] = cde and i have [32] = fge [13]

php merge two arrays

I have two arrays, I want to merge these two arrays into single array. Please view the detail below: First Array: Array ( [0] => Array ( [a] => 1 [b] => 2 [c] => 3 ) [1] => Array ( [a]

Keeping two arrays, String[] and int[], parallel with merge sort

I’m having trouble with keeping two arrays in parallel within my Merge Sorting algorithm. Suppose I have array defMergeSort and intMergeSort2. I would like to lexicographically order String defMergeSo

PHP function to check that two arrays are the same while ignoring the values of specified keys

I need a PHP function that can assert that two arrays are the same while ignoring the values of a specified set of keys (only the value, the keys must match). In practice, the arrays must have the sam

PHP, Merging arrays with common keys

I need to get two arrays to merge into one while keeping all the keys in place and listing the values in an array like this example; $array1 = array(‘car’ => ‘3’, ‘bus’ => ‘2’); $array2 = array(

Merge two arrays keeping the first array values as keys and second array values as values

Basically i have an array of key mappings (actually translations) and an array of data values. I want to essentially replace the keys of the second array with the values of the first array. E.G.: $arr

How do I merge two arrays with corresponding incremented keys in php?

I have a two arrays in php $arr1 = array(1=>332, 2=>333, 3=>74, 4=>78); $arr2 = array(1=>’heading1′, 3=>’heading2′); Note:Second array keys should not be changed. I need a output li

Answers

You can simply ‘add’ the arrays:

>> $a = array(1, 2, 3);
array (
  0 => 1,
  1 => 2,
  2 => 3,
)
>> $b = array("a" => 1, "b" => 2, "c" => 3)
array (
  'a' => 1,
  'b' => 2,
  'c' => 3,
)
>> $a + $b
array (
  0 => 1,
  1 => 2,
  2 => 3,
  'a' => 1,
  'b' => 2,
  'c' => 3,
)

Considering that you have

$replaced = array('1' => 'value1', '4' => 'value4');
$replacement = array('4' => 'value2', '6' => 'value3');

Doing $merge = $replacement + $replaced; will output:

Array('1' => 'value1', '4' => 'value2', '6' => 'value3');

The first array from sum will have values in the final output.

Doing $merge = $replaced + $replacement; will output:

Array('1' => 'value1', '4' => 'value4', '6' => 'value3');