We have two array.
First array contains some key and values.
Second array contains some keys and values.
I want to merge this two array if duplicate values occurred remove first array duplicate value only.
I want to check key and value is case insensitive.
Please use below code for these requirements.
Code :
<?php
$alname = array(
"CHE" => "Chennai" ,
"JKT" => "Jakarta" ,
"KOL" => "Kolkata" ,
"MDU" => "Madurai" ,
"MUM" => "Mumbai" ,
"SHG" => "Shanghai" );
$alname1 = array(
"TKY" => "Tokyo" ,
"MXI" => "Mexico City" ,
"DHI" => "Delhi" ,
"KAR" => "Karachi" ,
"CHN" => "CheNnai" ,
"ShG" => "ShaNghai" ,
"ISB" => "Istanbul" );
echo "<pre>";
echo "<br/>First Array : <br/>";
print_r($alname);
echo "<br/>Second Array : <br/>";
print_r($alname1);
$arr2 = array_merge($alname,$alname1);
echo "<br/>Merge first and second array Before remove duplicate values : <br/>";
print_r($arr2);
foreach($alname as $alkey => $alval)
{
$slow_key = strtolower($alkey);
$slow_val = strtolower($alval);
$slow_alname1 = unserialize(strtolower(serialize($alname1)));
$slow_alname1 = array_change_key_case($slow_alname1,CASE_LOWER);
if(isset($slow_alname1[$slow_key]))
{
unset($alname[$alkey]);
}
if(in_array($slow_val,$slow_alname1))
{
unset($alname[$alkey]);
}
}
$arr2 = array_merge($alname,$alname1);
echo "<br/><br/>Merge first and second array After remove duplicate values : <br/><br/>";
print_r($arr2);
echo "</pre>";
?>
OUTPUT :
First array contains some key and values.
Second array contains some keys and values.
I want to merge this two array if duplicate values occurred remove first array duplicate value only.
I want to check key and value is case insensitive.
Please use below code for these requirements.
Code :
<?php
$alname = array(
"CHE" => "Chennai" ,
"JKT" => "Jakarta" ,
"KOL" => "Kolkata" ,
"MDU" => "Madurai" ,
"MUM" => "Mumbai" ,
"SHG" => "Shanghai" );
$alname1 = array(
"TKY" => "Tokyo" ,
"MXI" => "Mexico City" ,
"DHI" => "Delhi" ,
"KAR" => "Karachi" ,
"CHN" => "CheNnai" ,
"ShG" => "ShaNghai" ,
"ISB" => "Istanbul" );
echo "<pre>";
echo "<br/>First Array : <br/>";
print_r($alname);
echo "<br/>Second Array : <br/>";
print_r($alname1);
$arr2 = array_merge($alname,$alname1);
echo "<br/>Merge first and second array Before remove duplicate values : <br/>";
print_r($arr2);
foreach($alname as $alkey => $alval)
{
$slow_key = strtolower($alkey);
$slow_val = strtolower($alval);
$slow_alname1 = unserialize(strtolower(serialize($alname1)));
$slow_alname1 = array_change_key_case($slow_alname1,CASE_LOWER);
if(isset($slow_alname1[$slow_key]))
{
unset($alname[$alkey]);
}
if(in_array($slow_val,$slow_alname1))
{
unset($alname[$alkey]);
}
}
$arr2 = array_merge($alname,$alname1);
echo "<br/><br/>Merge first and second array After remove duplicate values : <br/><br/>";
print_r($arr2);
echo "</pre>";
?>
OUTPUT :