Reputation: 1746
I have two arrays. And I want to merge them on the same key AND value. If they have the same ur_user_id
then they are merged. array2
only provides some additional data for array1
, so the new_array.length
= array1.length
. array1
just gets the additional data from array2
.
$array1 =
array(
array('ur_user_id'=> 1,'ur_fname'=>'PerA','ur_lname'=>'SonA'),
array('ur_user_id'=> 2,'ur_fname'=>'PerB','ur_lname'=>'SonB'),
array('ur_user_id'=> 3,'ur_fname'=>'PerC','ur_lname'=>'SonC'),
);
$array2 =
array(
array('ur_user_id' => 5,'ur_code' => 'EE','ur_user_role' => 'testE'),
array('ur_user_id' => 4,'ur_code' => 'DD','ur_user_role' => 'testD'),
array('ur_user_id' => 6,'ur_code' => 'FF','ur_user_role' => 'testF'),
array('ur_user_id' => 3,'ur_code' => 'CC','ur_user_role' => 'testC'),
array('ur_user_id' => 1,'ur_code' => 'AA','ur_user_role' => 'testA'),
array('ur_user_id' => 2,'ur_code' => 'BB','ur_user_role' => 'testB'),
);
Then the new array must look like this. It will have both the values from the array1
and array2
.
$new_array =
array(
array('ur_user_id'=> 1,'ur_fname'=>'PerA','ur_lname'=>'SonA','ur_code' => 'AA','ur_user_role' => 'testA'),
array('ur_user_id'=> 2,'ur_fname'=>'PerB','ur_lname'=>'SonB','ur_code' => 'BB','ur_user_role' => 'testB'),
array('ur_user_id'=> 3,'ur_fname'=>'PerC','ur_lname'=>'SonC','ur_code' => 'CC','ur_user_role' => 'testC'),
);
The array1.length
is always less than or equal to array2.length
never greater. And the order of both arrays will not be always ordered. I've tried the function below which I got somewhere here but it doesn't work for me and I'm not really good with loops.
function merge_common_keys(){
$arr = func_get_args();
$num = func_num_args();
$keys = array();
$i = 0;
for ($i=0; $i<$num; ++$i){
$keys = array_merge($keys, array_keys($arr[$i]));
}
$keys = array_unique($keys);
$merged = array();
foreach ($keys as $key){
$merged[$key] = array();
for($i=0; $i<$num; ++$i){
$merged[$key][] = isset($arr[$i][$key]) ? $arr[$i][$key] : null;
}
}
return $merged;
}
Based on the given arrays the result is like this. It only merges on the same key.
Array
(
[0] => Array
(
[0] => Array
(
[ur_user_id] => 1
[ur_fname] => PerA
[ur_lname] => SonA
)
[1] => Array
(
[ur_user_id] => 5
[ur_code] => AA-BB-CC
[ur_user_role] => testE
)
)
Upvotes: 6
Views: 8403
Reputation: 47864
When you need to make iterated checks/searches for values (effectively a unique identifier), the best performing approach will often:
isset()
(instead of in_array()
or array_search()
)ur_user_id
is your "unique identifier".
$array2
is prepared by assigning ur_user_id
values as keys with array_column()
.
$array1
controls the number of iterations.
My approach will check for a corresponding row between $array1
and $array2
(to avoid Notices) before using a union operator (+=
) to append the $array2
data to each original row of $array1
.
The &
in the foreach loop "modifies by reference" -- this means the actual input array is being "dealt with" instead of a copy of the input array.
Code: (Demo)
$array1 =
array(
array('ur_user_id'=> 1,'ur_fname'=>'PerA','ur_lname'=>'SonA'),
array('ur_user_id'=> 2,'ur_fname'=>'PerB','ur_lname'=>'SonB'),
array('ur_user_id'=> 3,'ur_fname'=>'PerC','ur_lname'=>'SonC'),
);
$array2 =
array(
array('ur_user_id' => 5,'ur_code' => 'EE','ur_user_role' => 'testE'),
array('ur_user_id' => 4,'ur_code' => 'DD','ur_user_role' => 'testD'),
array('ur_user_id' => 6,'ur_code' => 'FF','ur_user_role' => 'testF'),
array('ur_user_id' => 3,'ur_code' => 'CC','ur_user_role' => 'testC'),
array('ur_user_id' => 1,'ur_code' => 'AA','ur_user_role' => 'testA'),
array('ur_user_id' => 2,'ur_code' => 'BB','ur_user_role' => 'testB'),
);
$keyed = array_column($array2, NULL, 'ur_user_id'); // replace indexes with ur_user_id values
foreach ($array1 as &$row) { // write directly to $array1 while iterating
if (isset($keyed[$row['ur_user_id']])) { // check if shared key exists
$row += $keyed[$row['ur_user_id']]; // append associative elements
}
}
var_export($array1);
Output:
array (
0 =>
array (
'ur_user_id' => 1,
'ur_fname' => 'PerA',
'ur_lname' => 'SonA',
'ur_code' => 'AA',
'ur_user_role' => 'testA',
),
1 =>
array (
'ur_user_id' => 2,
'ur_fname' => 'PerB',
'ur_lname' => 'SonB',
'ur_code' => 'BB',
'ur_user_role' => 'testB',
),
2 =>
array (
'ur_user_id' => 3,
'ur_fname' => 'PerC',
'ur_lname' => 'SonC',
'ur_code' => 'CC',
'ur_user_role' => 'testC',
),
)
Upvotes: 2
Reputation: 2995
Try this three line code in foreach loop like this :
$array1 =
array(
array('ur_user_id'=> 1,'ur_fname'=>'PerA','ur_lname'=>'SonA'),
array('ur_user_id'=> 2,'ur_fname'=>'PerB','ur_lname'=>'SonB'),
array('ur_user_id'=> 3,'ur_fname'=>'PerC','ur_lname'=>'SonC'),
);
$array2 =
array(
array('ur_user_id' => 5,'ur_code' => 'EE','ur_user_role' => 'testE'),
array('ur_user_id' => 4,'ur_code' => 'DD','ur_user_role' => 'testD'),
array('ur_user_id' => 6,'ur_code' => 'FF','ur_user_role' => 'testF'),
array('ur_user_id' => 3,'ur_code' => 'CC','ur_user_role' => 'testC'),
array('ur_user_id' => 1,'ur_code' => 'AA','ur_user_role' => 'testA'),
array('ur_user_id' => 2,'ur_code' => 'BB','ur_user_role' => 'testB'),
);
$newArray =array();
foreach($array1 as $key => $val)
{
$ids = array_map(function ($ar) {return $ar['ur_user_id'];}, $array2); //get the all the user ids from array 2
$k = array_search($val['ur_user_id'],$ids); // find the key of user id in ids array
$newArray[] = array_merge($array1[$key],$array2[$k]); /// merge the first array key with second
}
echo "<pre>"; print_r($newArray);
This will give you :
(
[0] => Array
(
[ur_user_id] => 1
[ur_fname] => PerA
[ur_lname] => SonA
[ur_code] => AA
[ur_user_role] => testA
)
[1] => Array
(
[ur_user_id] => 2
[ur_fname] => PerB
[ur_lname] => SonB
[ur_code] => BB
[ur_user_role] => testB
)
[2] => Array
(
[ur_user_id] => 3
[ur_fname] => PerC
[ur_lname] => SonC
[ur_code] => CC
[ur_user_role] => testC
)
)
Upvotes: 1
Reputation: 1438
Try out this code, It might help you, it's short and without traversing loops:
usort($array2,function($a,$b){
return strnatcmp($a['ur_user_id'],$b['ur_user_id']);
});
$array3 = array_replace_recursive($array1, $array2);
$result = array_uintersect($array3,$array1,function($a,$b){
return strnatcmp($a['ur_user_id'],$b['ur_user_id']);
});
print_r($result);
Output
Array
(
[0] => Array
(
[ur_user_id] => 1
[ur_fname] => PerA
[ur_lname] => SonA
[ur_code] => AA
[ur_user_role] => testA
)
[1] => Array
(
[ur_user_id] => 2
[ur_fname] => PerB
[ur_lname] => SonB
[ur_code] => BB
[ur_user_role] => testB
)
[2] => Array
(
[ur_user_id] => 3
[ur_fname] => PerC
[ur_lname] => SonC
[ur_code] => CC
[ur_user_role] => testC
)
)
And Here is Demo
Upvotes: 1
Reputation: 1981
Try this:
<?php
$array1 =
array(
array('ur_user_id'=> 1,'ur_fname'=>'PerA','ur_lname'=>'SonA'),
array('ur_user_id'=> 2,'ur_fname'=>'PerB','ur_lname'=>'SonB'),
array('ur_user_id'=> 3,'ur_fname'=>'PerC','ur_lname'=>'SonC'),
);
$array2 =
array(
array('ur_user_id' => 5,'ur_code' => 'EE','ur_user_role' => 'testE'),
array('ur_user_id' => 4,'ur_code' => 'DD','ur_user_role' => 'testD'),
array('ur_user_id' => 6,'ur_code' => 'FF','ur_user_role' => 'testF'),
array('ur_user_id' => 3,'ur_code' => 'CC','ur_user_role' => 'testC'),
array('ur_user_id' => 1,'ur_code' => 'AA','ur_user_role' => 'testA'),
array('ur_user_id' => 2,'ur_code' => 'BB','ur_user_role' => 'testB'),
);
$result = array();
$userIdsFromArray1 = array_column($array1, 'ur_user_id');
foreach($array2 as $subarray)
{
if(in_array($subarray['ur_user_id'], $userIdsFromArray1))
{
$result[] = array_merge($subarray, getDataFromArray1ByUserId($array1, $subarray['ur_user_id']));
}
}
function getDataFromArray1ByUserId($array1, $userId)
{
foreach($array1 as $key => $data)
{
if($data['ur_user_id'] == $userId)
return $data;
}
return array();
}
usort($result,function($a,$b){
return strnatcmp($a['ur_user_id'],$b['ur_user_id']);
});
print_r($result);
Working example : CLICK!
Upvotes: 1
Reputation: 9583
You better go with this:
$ids = array();
$out = array();
foreach($array1 as $key => $value){
if(isset($array2[$key]))
$out[$key][] = $array2[$key];
$out[$key][] = $value;
$ids[] = $array2[$key]['ur_user_id'];
}
foreach($array2 as $key => $val){
if(!in_array($val['ur_user_id'], $ids))
$out[$key][] = $array2[$key];
}
Upvotes: -1