给定键上的二维PHP数组的差异

时间:2017-05-09 15:40:46

标签: php arrays

我有两个数组。一个来自数据库,我可以订购我想要的 第二个来自第三方api,我无法控制订单。我正在尝试根据三个键(pk1pk2pk3)确定差异,如$desired下所示。

$array1=[
    ["pk1"=>1,"pk2"=>1,"pk3"=>1,"attr1"=>1,"attr2"=>3],
    ["pk1"=>1,"pk2"=>2,"pk3"=>1,"attr1"=>3,"attr2"=>1],
    ["pk1"=>1,"pk2"=>1,"pk3"=>2,"attr1"=>2,"attr2"=>3],
];

$array2=[
    ["pk1"=>2,"pk2"=>2,"pk3"=>1,"attr1"=>3,"attr2"=>1],
    ["pk1"=>1,"pk3"=>2,"pk2"=>1,"attr1"=>4,"attr2"=>3],
    ["pk1"=>1,"pk2"=>1,"pk3"=>1,"attr1"=>1,"attr2"=>3],
];

$desired=[
    ["pk1"=>1,"pk2"=>1,"pk3"=>1,"array1"=>["attr1"=>1,"attr2"=>3],"array2"=>["attr1"=>1,"attr2"=>3]],
    ["pk1"=>1,"pk2"=>2,"pk3"=>1,"array1"=>["attr1"=>3,"attr2"=>1],"array2"=>null],
    ["pk1"=>1,"pk2"=>1,"pk3"=>2,"array1"=>["attr1"=>2,"attr2"=>3],"array2"=>["attr1"=>4,"attr2"=>3]],
    ["pk1"=>2,"pk2"=>2,"pk3"=>1,"array1"=>null,"array2"=>["attr1"=>3,"attr2"=>1]]
];

2 个答案:

答案 0 :(得分:1)

你好,你打败了我!但是,假设您的解决方案正常运行,那么我对此有所了解。

<?php

$array1=[
    ["pk1"=>1,"pk2"=>1,"pk3"=>1,"attr1"=>1,"attr2"=>3],
    ["pk1"=>1,"pk2"=>2,"pk3"=>1,"attr1"=>3,"attr2"=>1],
    ["pk1"=>1,"pk2"=>1,"pk3"=>2,"attr1"=>2,"attr2"=>3],
];

$array2=[
    ["pk1"=>2,"pk2"=>2,"pk3"=>1,"attr1"=>3,"attr2"=>1],
    ["pk1"=>1,"pk3"=>2,"pk2"=>1,"attr1"=>4,"attr2"=>3],
    ["pk1"=>1,"pk2"=>1,"pk3"=>1,"attr1"=>1,"attr2"=>3],
];

$desired=[
    ["pk1"=>1,"pk2"=>1,"pk3"=>1,"array1"=>["attr1"=>1,"attr2"=>3],"array2"=>["attr1"=>1,"attr2"=>3]],
    ["pk1"=>1,"pk2"=>2,"pk3"=>1,"array1"=>["attr1"=>3,"attr2"=>1],"array2"=>null],
    ["pk1"=>1,"pk2"=>1,"pk3"=>2,"array1"=>["attr1"=>2,"attr2"=>3],"array2"=>["attr1"=>4,"attr2"=>3]],
    ["pk1"=>2,"pk2"=>2,"pk3"=>1,"array1"=>null,"array2"=>["attr1"=>3,"attr2"=>1]]
];
echo('$array1');print_r($array1);
echo('$array2');print_r($array2);
echo('$desired');print_r($desired);

$array=[];
foreach($array1 as $arr) {
    $array["$arr[pk1]~$arr[pk2]~$arr[pk3]"]=["pk1"=>$arr['pk1'],"pk2"=>$arr['pk2'],"pk3"=>$arr['pk3'],"array1"=>["attr1"=>$arr['attr1'],"attr2"=>$arr['attr2']],"array2"=>null];
}
foreach($array2 as $arr) {
    if(isset($array["$arr[pk1]~$arr[pk2]~$arr[pk3]"])) {
        $array["$arr[pk1]~$arr[pk2]~$arr[pk3]"]['array2']=["attr1"=>$arr['attr1'],"attr2"=>$arr['attr2']];
    }
    else {
        $array[]=["pk1"=>$arr['pk1'],"pk2"=>$arr['pk2'],"pk3"=>$arr['pk3'],"array1"=>null, "array2"=>["attr1"=>$arr['attr1'],"attr2"=>$arr['attr2']]];
    }
}
$array=array_values($array);
echo('$array');print_r($array);

答案 1 :(得分:0)

使用从pk创建的唯一键的完整解决方案 - 键:

// delimiter for creating unique keys
const DELIMITER = '~';

// source arrays
$array1=[
    ["pk1"=>1,"pk2"=>1,"pk3"=>1,"attr1"=>1,"attr2"=>3],
    ["pk1"=>1,"pk2"=>2,"pk3"=>1,"attr1"=>3,"attr2"=>1],
    ["pk1"=>1,"pk2"=>1,"pk3"=>2,"attr1"=>2,"attr2"=>3],
];

$array2=[
    ["pk1"=>2,"pk2"=>2,"pk3"=>1,"attr1"=>3,"attr2"=>1],
    ["pk1"=>1,"pk3"=>2,"pk2"=>1,"attr1"=>4,"attr2"=>3],
    ["pk1"=>1,"pk2"=>1,"pk3"=>1,"attr1"=>1,"attr2"=>3],
];

// function to create new arrays with unique keys from source arrays
function doSmth($arr) 
{
    $result = [];

    foreach ($arr as $item) {
        // new key is created via concatenating pk-keys
        $result[$item["pk1"] . DELIMITER . $item["pk2"] . DELIMITER . $item["pk3"]] = [
            "attr1"=>$item["attr1"],
            "attr2"=>$item["attr2"],
        ];
    }

    return $result;
}

// creating new arrays
$narr1 = doSmth($array1);
$narr2 = doSmth($array2);

// target array
$desired = [];
foreach ($narr1 as $k => $v) {
    // restore pk-keys from $k
    $parts = explode(DELIMITER, $k);
    $desired[] = [
        'pk1' => $parts[0],
        'pk2' => $parts[1],
        'pk3' => $parts[2],
        'array1' => $v,
        // if same key exists in `$narr2` - use it
        'array2' => isset($narr2[$k])? $narr2[$k] : null
    ];
    if (isset($narr2[$k])) {
        // unset value from `$narr2`
        unset($narr2[$k]);
    }
}

// if something left in `$narr2` - add it to target array
foreach ($narr2 as $k => $v) {
    $parts = explode(DELIMITER, $k);
    $desired[] = [
        'pk1' => $parts[0],
        'pk2' => $parts[1],
        'pk3' => $parts[2],
        'array1' => null,
        'array2' => $v,
    ];
}

echo'<pre>',print_r($desired),'</pre>';