2012-02-03 128 views
1

我從嵌套數組中提取重複值。我想從$ bigarray中刪除這些已經被剔除的項目。你會給我一些想法嗎?PHP:從嵌套數組中刪除值

$bigarray = array(                                                               
    "430" => array('milk', 'turky', 'apple'), 
    "433" => array('milk', 'apple', 'orange', 'england'), 
    "444" => array('milk', 'apple', 'orange', 'spain') 
); 
$intersected = null; 
foreach ($bigarray as $arr) { 
    $intersected = $intersected ? array_intersect($arr, $intersected) : $arr; 
    if (!$intersected) { 
    break; // no reason to continue 
    } 
} 


foreach ($intersected as $inter){ 
    foreach ($bigarray as $arr) { 
    foreach ($arr as $value=>$key) { 
     if ($key == $inter){ 
     unset($arr[$value]); 
     } 
    } 
    //print_r($arr); 
    } 
} 
print_r($bigarray); 
+0

說有什麼錯示例代碼將是一個良好的開端... – 2012-02-03 14:33:19

+0

是的,我的「M提取重複,但不能刪除它們。我想從原始數組中提取並刪除它們。 – user973067 2012-02-03 14:35:13

+0

一旦你知道了重複項是什麼(循環退出後'$ intersected'),你就可以得到這個結果並返回到原始數組並刪除這些值。 – 2012-02-03 14:37:13

回答

0

這是你在找什麼?

foreach($bigarray as $id => $arr) 
    $bigarray[$id] = array_unique($arr); 
1

你應該看看array_merge,因爲它會合並2列在一起,只保留一個副本。從手冊:

如果輸入數組具有相同的字符串鍵,則該鍵的後面的值 將覆蓋前一個。但是,如果數組鍵 包含數字鍵,則後面的值不會覆蓋原始值 值,但會被追加。

聽起來像功課,問題還不是很清楚,所以這是所有我可以提供了。

0

我不完全瞭解你的問題,但使用array_unique()數組中,我得到了以下的輸出:

array(1) { 
    [430]=> 
    array(3) { 
    [0]=> 
    string(4) "milk" 
    [1]=> 
    string(5) "turky" 
    [2]=> 
    string(5) "Apple" 
    } 
} 

也許這可能是acchieving你想要的方式。

0
function array_unique_nested($arr=array(),$matched=array(),$cm=false){ 
foreach($arr as $i=>$v){ 
if (is_array($v)) {$arr[$i]=array_unique_nested($v,$matched,false); 
$matched=array_unique_nested($v,$matched,true); continue;} 
if (in_array($v,$matched)) {unset($arr[$i]);continue;} 
$matched[]=$v;} 
if ($cm) return $matched; 
else return $arr;} 

如果不起作用,這個http://php.net/manual/en/function.array-unique.php的代碼片段應該做的伎倆。

if(!function_exists('array_flat')) 
{ 
    function array_flat($a, $s = array(), $l = 0) 
{ 
    # check if this is an array 
    if(!is_array($a))       return $s; 

    # go through the array values 
    foreach($a as $k => $v) 
    { 
     # check if the contained values are arrays 
     if(!is_array($v)) 
     { 
      # store the value 
      $s[ ]  = $v; 

      # move to the next node 
      continue; 

     } 

     # increment depth level 
     $l++; 

     # replace the content of stored values 
     $s    = array_flat($v, $s, $l); 

     # decrement depth level 
     $l--; 

    } 

    # get only unique values 
    if($l == 0) $s = array_values(array_unique($s)); 

    # return stored values 
    return $s; 

} # end of function array_flat(... 

}

0

可以使用array_unique($陣列[摘要$ sort_flags改變]功能。如果不指定可選sort_flag,該函數將比較值轉換一切字符串。如果你具有比陣列中的字符串的其它值,則可以指定sort_flag是下列值之一

SORT_REGULAR - compare items normally (don't change types) 
SORT_NUMERIC - compare items numerically 
SORT_STRING - compare items as strings 
SORT_LOCALE_STRING - compare items as strings, based on the current locale. 

從PHP.net

  $input = array("a" => "green", "red", "b" => "green", "blue", "red"); 
     $result = array_unique($input); 
     print_r($result); 
實施例

的更多信息,請參閱 http://php.net/manual/en/function.array-unique.php