2012-09-28 31 views
1

我有對象的multidimensionnal數組中刪除重複的鍵:從multidimensionnal陣列

0 => 
    array 
     32281 => object ... 
     105145 => object ... 
     165656 => object ... 
     194124 => object ... 
     195397 => object ... 
     205859 => object ... 
    1 => 
    array 
     32281 => object ... 
     91504 => object ... 
     165656 => object ... 
     194124 => object ... 
     195397 => object ... 
     205859 => object ... 
    3 => 
    array 
     32281 => object ... 
     105145 => object ... 
     165656 => object ... 
     194124 => object ... 
     195397 => object ... 
     205859 => object ... 

和我想從這個數組像這樣(在這種情況下刪除重複的陣列我將刪除1,只是有0和3,因爲0和1相同):

 0 => 
     array 
      32281 => object ... 
      105145 => object ... 
      165656 => object ... 
      194124 => object ... 
      195397 => object ... 
      205859 => object ... 
     3 => 
     array 
      32281 => object ... 
      91504 => object ... 
      165656 => object ... 
      194124 => object ... 
      195397 => object ... 
      205859 => object ... 

我已經失敗嘗試了很多事情array_unique,array_keys,array_keys_exists ...

例如:

$array = array_map("unserialize", array_unique(array_map("serialize", $array))); 

$result = array();  
foreach ($array as $key => $value) { 
    if(!array_key_exists($key,$result)) 
     $result[$key] = $array[$key]; 
    } 
+1

等你仍然有一個重複數組1.問題:你如何獲得數組?也許你可以在不重複的情況下創建它,而不是稍後循環搜索重複項。 –

回答

3

這個功能應該做的:

function my_unique($array) { 
    foreach($array as $key => $value) { 
    foreach($array as $key2 => $value2) { 
     if($key != $key2 && $value === $value2) { 
     unset($array[$key]); 
     } 
    } 
    } 
    return $array; 
} 
+0

非常感謝你,這工作正常! – user1705785

+0

很好的把戲 – Deep

0

您可以嘗試

$array = array(
    0 => 
    array (
     32281 => new stdClass , 
     105145 => new stdClass , 
     165656 => new stdClass , 
     194124 => new stdClass , 
     195397 => new stdClass , 
     205859 => new stdClass), 
    1 => 
    array (
     32281 => new stdClass , 
     91504 => new stdClass , 
     165656 => new stdClass , 
     194124 => new stdClass , 
     195397 => new stdClass , 
     205859 => new stdClass) , 
    3 => 
    array (
     32281 => new stdClass , 
     105145 => new stdClass , 
     165656 => new stdClass , 
     194124 => new stdClass , 
     195397 => new stdClass , 
     205859 => new stdClass ) 
    ); 


$array2 = array_unique(array_map("serialize", $array)); 
$final = array_map("unserialize",array_diff($array2, array_diff_assoc(array_map("serialize", $array), $array2))); 
var_dump($final); 

輸出

array 
    1 => 
    array 
     32281 => 
     object(stdClass)[19] 
     91504 => 
     object(stdClass)[20] 
     165656 => 
     object(stdClass)[21] 
     194124 => 
     object(stdClass)[22] 
     195397 => 
     object(stdClass)[23] 
     205859 => 
     object(stdClass)[24] 
+1

LOL,你也使用stdClass。 –

+0

看我的解決方案,array_walk_recursive效果更好。 –

+0

THX,但我don'h有最後陣列 '3 => 陣列( 32281 =>新stdClass的, 105145 =>新stdClass的, 165656 =>新stdClass的, 194124 =>新stdClass的, 195397 =>新的stdClass, 205859 =>新的stdClass) );' – user1705785

0
<?php 
$arr = array (0 => 
    array(
     32281 => new stdClass, 
     105145 => new stdClass, 
     165656 => new stdClass, 
     194124 => new stdClass, 
     195397 => new stdClass, 
     205859 => new stdClass, 
    ), 
    1 => 
    array(
     32281 => new stdClass, 
     91504 => new stdClass, 
     165656 => new stdClass, 
     194124 => new stdClass, 
     195397 => new stdClass, 
     205859 => new stdClass, 
    ), 
    3 => 
    array(
     32281 => new stdClass, 
     105145 => new stdClass, 
     165656 => new stdClass, 
     194124 => new stdClass, 
     195397 => new stdClass, 
     205859 => new stdClass, 
    ), 
); 

$result = array(); 
function put($value, $key) { 
    global $result; 
    $result[$key] = $value; 
} 
array_walk_recursive($arr, "put"); 

var_dump($result); 

/** 
array(7) { 
    [32281]=> 
    object(stdClass)#13 (0) { 
    } 
    [105145]=> 
    object(stdClass)#14 (0) { 
    } 
    [165656]=> 
    object(stdClass)#15 (0) { 
    } 
    [194124]=> 
    object(stdClass)#16 (0) { 
    } 
    [195397]=> 
    object(stdClass)#17 (0) { 
    } 
    [205859]=> 
    object(stdClass)#18 (0) { 
    } 
    [91504]=> 
    object(stdClass)#8 (0) { 
    } 
} 

*/ 
+0

謝謝,但我得到一個這個解決方案的空陣列 – user1705785

+0

@ user1705785你確定嗎?我的測試是正確的。 –

+0

是的,輸出是:**數組**'空' – user1705785

0

如果我理解你的問題正確的話,你想刪除重複的子陣基地的所有鍵

假設你有一個自然的0基於陣列的基礎上重點:

$keys=array_map("serialize",array_map("array_keys",$arr)); 
$keys=array_unique($keys); 
$result=array(); 
foreach($keys as $idx=>$not_care) 
{ 
    $result[$idx]=$arr[$idx]; 
} 

所以對於

$arr=array(array(1234=>"1234",5678=>"5678"), 
    array(1357=>"1357",2468=>"2468"), 
    array(1234=>"1234",5678=>"5678"), 
    array(1357=>"1357",8642=>"8642")); 

你:

Array 
(
    [0] => Array 
     (
      [1234] => 1234 
      [5678] => 5678 
     ) 

    [1] => Array 
     (
      [1357] => 1357 
      [2468] => 2468 
     ) 

    [3] => Array 
     (
      [1357] => 1357 
      [8642] => 8642 
     ) 

) 
+0

這是行不通的,因爲我有一個對象數組: '$ arr = array(array(1234 =>「1234」,5678 =>「5678」)'在我的情況下是: '$ arr = array(array(1234 => object(v => 1,id => 1234,...),5678 => object(v => 2,id => 5678)' – user1705785

+0

@ user1705785我無法看到您的例如子數組元素的_value_被考慮,所以我不認爲它;實際上,它是一個對象還是一個簡單的字符串是無關緊要的 - 你可以看到我的代碼只用了子數組元素的_keys_考慮到,正如我在答覆中所說的那樣。 – Passerby