2017-05-09 53 views
0

我有兩個數組。一個來自數據庫,我可以訂購我想要的。
第二個來自第三方api,我無法控制訂單。我試圖根據三個密鑰(pk1,pk2pk3)確定差異,如$desired下所示。採用獨特的鍵給定鍵上的二維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]] 
]; 
+0

你將不得不詳細解釋你的問題,它的代碼並不明顯 – Steve

+0

從'pk's創建唯一的密鑰並進行比較。 –

+0

@u_mulder我現在開始這樣做,使用'$ array [「$ arr [pk1]〜$ arr [pk2]〜$ arr [pk3]」]'這是你的意思嗎? – user1032531

回答

0

完整的解決方案,從pk -keys創建:

// 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>'; 
1

u_mulder,你打我吧!然而,假設你的解決方案有效,我會和你一樣,更像你的解決方案。

<?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);