Php same key value pair array merge

the following two arrays are used to find the best merging method

$arr_1 = [
    ["id"=>1,"cc"=>"dd","ee"=>"hh"],
    ["id"=>2,"cc"=>"gg","ee"=>"qq"],
    ["id"=>3,"cc"=>"yy","ee"=>""],
];
$arr_2 = [
    ["id"=>2,"hh"=>"-sharp-sharp","ll"=>"^^"],
    ["id"=>1,"hh"=>"@@","ll"=>"%%"],
    ["id"=>4,"hh"=>"$$","ll"=>"&&"],
];
$arr = [];
foreach ($arr_1 as $key => &$value) {
    foreach ($arr_2 as $k => $item) {
        if ($value["id"] === $item["id"]) {
            $temp = array_merge($value,$item);
            $arr[] = $temp;
        }
    }
}

$temp_ids = array_column($arr, "id");

$sub_1 = array_filter($arr_1,function($o) use ($temp_ids){
    if (!in_array($o["id"], $temp_ids)) {
        return true;
    }
});


$sub_2 = array_filter($arr_2,function($i) use ($temp_ids){
    if (!in_array($i["id"], $temp_ids)) {
        return true;
    }
});

$res = array_merge($arr,$sub_1,$sub_2);
echo json_encode($res);
exit;

result

[{"id":1,"cc":"dd","ee":"hh","hh":"@@","ll":"%%"},{"id":2,"cc":"gg","ee":"qq","hh":"-sharp-sharp","ll":"^^"},{"id":3,"cc":"yy","ee":"\uff01\uff01"},{"id":4,"hh":"$$","ll":"&&"}]
Php
May.22,2021

actually, it's pretty good for the author to write that.
if you want to use an imposing comparison sentence, you can do this

.
function compare_id($a,$b){
    return $a['id'] - $b['id'];
}

$intersect_a = array_uintersect($arr_1, $arr_2, 'compare_id');
$intersect_b = array_uintersect($arr_2, $arr_1, 'compare_id');
$diff_a = array_udiff($arr_1, $arr_2, 'compare_id');
$diff_b = array_udiff($arr_2, $arr_1, 'compare_id');
usort($intersect_a, 'compare_id');
usort($intersect_b, 'compare_id');

echo json_encode(array_merge(array_map('array_merge', $intersect_a, $intersect_b), $diff_a,$diff_b));

Let me also show cool techs

        $a1 = array_combine(array_column($arr_1, 'id'), $arr_1);
        $a2 = array_combine(array_column($arr_2, 'id'), $arr_2);
        $result = [];
        foreach ($a1 as $id => $item) {
            if (key_exists($id, $a2)) {
                $result[] = array_merge($item, $a2[$id]);
                unset($a2[$id]);
            } else {
                $result[] = $item;
            }
        }
        $result = array_merge($result, $a2 ?: []);
        array_multisort(array_column($result, 'id'), SORT_ASC, $result); 
        dump($result);
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e59262-59a93.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e59262-59a93.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?