Merging arrays with the same keys

PhpArray Merge

Php Problem Overview


In a piece of software, I merge two arrays with array_merge function. But I need to add the same array (with the same keys, of course) to an existing array.

The problem:

 $A = array('a' => 1, 'b' => 2, 'c' => 3);
 $B = array('c' => 4, 'd'=> 5);
 
 array_merge($A, $B);

 // result
 [a] => 1 [b] => 2 [c] => 4 [d] => 5

As you see, 'c' => 3 is missed.

So how can I merge all of them with the same keys?

Php Solutions


Solution 1 - Php

You need to use array_merge_recursive instead of array_merge. Of course there can only be one key equal to 'c' in the array, but the associated value will be an array containing both 3 and 4.

Solution 2 - Php

Try with array_merge_recursive

$A = array('a' => 1, 'b' => 2, 'c' => 3);
$B = array('c' => 4, 'd'=> 5);
$c = array_merge_recursive($A,$B);

echo "<pre>";
print_r($c);
echo "</pre>";

will return

Array
(
    [a] => 1
    [b] => 2
    [c] => Array
        (
            [0] => 3
            [1] => 4
        )

    [d] => 5
)

Solution 3 - Php

$arr1 = array(
   "0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
   "1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
   "2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
   "3" => array("fid" => 1, "tid" => 7, "name" => "Xi"),
   "4" => array("fid" => 2, "tid" => 9, "name" => "Xigua")
);

if you want to convert this array as following:

$arr2 = array(
   "0" => array(
          "0" => array("fid" => 1, "tid" => 1, "name" => "Melon"),
		  "1" => array("fid" => 1, "tid" => 4, "name" => "Tansuozhe"),
		  "2" => array("fid" => 1, "tid" => 6, "name" => "Chao"),
		  "3" => array("fid" => 1, "tid" => 7, "name" => "Xi")
	),
   
    "1" => array(
          "0" =>array("fid" => 2, "tid" => 9, "name" => "Xigua")
     )
);

so, my answer will be like this:

$outer_array = array();
$unique_array = array();
foreach($arr1 as $key => $value)
{
    $inner_array = array();
	
	$fid_value = $value['fid'];
	if(!in_array($value['fid'], $unique_array))
	{
			array_push($unique_array, $fid_value);
			unset($value['fid']);
			array_push($inner_array, $value);
			$outer_array[$fid_value] = $inner_array;
			
			
	}else{
	        unset($value['fid']);
			array_push($outer_array[$fid_value], $value);
		
	}
}
var_dump(array_values($outer_array));

hope this answer will help somebody sometime.

Solution 4 - Php

 $A = array('a' => 1, 'b' => 2, 'c' => 3);
 $B = array('c' => 4, 'd'=> 5);
 $C = array_merge_recursive($A, $B);
 $aWhere = array();
 foreach ($C as $k=>$v) {
 
	if (is_array($v)) {
		$aWhere[] = $k . ' in ('.implode(', ',$v).')';
	}
	else {
		$aWhere[] = $k . ' = ' . $v;
	}
 }
 $where = implode(' AND ', $aWhere);
 echo $where;

Solution 5 - Php

I just wrote this function, it should do the trick for you, but it does left join

public function mergePerKey($array1,$array2)
	{
		$mergedArray = [];

		foreach ($array1 as $key => $value) 
		{
            if(isset($array2[$key]))
             {
               $mergedArray[$value] = null;
               
               continue;
             }
			$mergedArray[$value] = $array2[$key];
		}

		return $mergedArray;
	}

Solution 6 - Php

Two entries in an array can't share a key, you'll need to change the key for the duplicate

Solution 7 - Php

Try this:

$array_one = ['ratio_type'];
$array_two = ['ratio_type', 'ratio_type'];
$array_three = ['ratio_type'];
$array_four = ['ratio_type'];
$array_five = ['ratio_type', 'ratio_type', 'ratio_type'];
$array_six = ['ratio_type'];
$array_seven = ['ratio_type'];
$array_eight = ['ratio_type'];

$all_array_elements = array_merge_recursive(
    $array_one,
    $array_two,
    $array_three,
    $array_four,
    $array_five,
    $array_six,
    $array_seven,
    $array_eight
);

foreach ($all_array_elements as $key => $value) {
    echo "[$key]" . ' - ' . $value . PHP_EOL;
}

// OR
var_dump($all_array_elements);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionkuzey beytarView Question on Stackoverflow
Solution 1 - PhpJonView Answer on Stackoverflow
Solution 2 - PhpdiEchoView Answer on Stackoverflow
Solution 3 - PhpMelonView Answer on Stackoverflow
Solution 4 - PhpNemodenView Answer on Stackoverflow
Solution 5 - PhpKamaroView Answer on Stackoverflow
Solution 6 - PhpMild FuzzView Answer on Stackoverflow
Solution 7 - PhpRadhasonView Answer on Stackoverflow