2012-01-23 209 views
0

我有一個多維數組看起來像這樣如果重複從數組中刪除多維元素的值?

$input = Array 
         (
          [222] => Array 
           (
            'id' => 222 
            'val' => 'def' 
            'more' => 'test' 
          ), 
         [223] => Array 
           (
            'id' => 223 
            'val' => 'aaa' 
            'more' => 'aaa' 
          ), 
         [224] => Array 
           (
            'id' => 224 
            'val' => 'aaa' 
            'more' => 'ddd' 
          ) 
         [225] => Array 
           (
            'id' => 225 
            'val' => 'def' 
            'more' => 'test' 
          ) 
      ); 

*複製由於其鍵valmore的值複製

我想刪除重複的值(在這種情況下,$輸入[222]和$輸入[225]。所以結果應該只

$res = Array 
         (
          [222] => Array 
           (
            'id' => 222 
            'val' => 'def' 
            'more' => 'test' 
          ), 
         [223] => Array 
           (
            'id' => 223 
            'val' => 'aaa' 
            'more' => 'aaa' 
          ), 
         [224] => Array 
           (
            'id' => 224 
            'val' => 'aaa' 
            'more' => 'ddd' 
          ) 
      ); 

任何人都知道如何做到這一點?

感謝

+0

你不得不(通過數組循環,不斷遇到ID的列表,並覈對列表中的每個條目)來做到這一點編程。但是,如果您從數據庫獲取此數據,則可能可以修改查詢以刪除重複項。 – Tom

+0

是的,我從數據庫中得到它。 – sophie

+0

嘗試使用'GROUP'和'ORDER'來獲得你想要的結果。就像這樣:'SELECT id,val,more FROM表GROUP BY id ORDER BY [時間戳或其他順序確定字段]' – Tom

回答

2
$newInput = array(); 
foreach($input as $k => $v) $newInput[$v['val'].$v['more']] = $v; 
//You may stop here. 
$newInputNum = array(); 
foreach($newInput as $k => $v) $newInputNum[$v['id']] = $v; 

echo "<pre>";print_r($newInputNum );echo "</pre>";