2014-11-04 110 views
2

我會提到我的數組波紋管。我需要從數組中刪除重複鍵,但我也需要保留其他元素合併。我知道這不夠清楚,所以我會解釋。如何從多維數組中刪除一些重複的元素

這是我想要的預期結果。

Main Cat Items 1 -> Categories 1 --> Sub Cat Items 1 
        Categories 1 --> Sub Cat Items 2 
        Categories 2 --> Sub Cat Items 3 
        Categories 3 --> Sub Cat Items 4 

Main Cat Items 2 -> Categories 4 --> Sub Cat Items 5 

但從查詢中我得到了一些這樣的事情。

Main Cat Items 1 -> Categories 1 --> Sub Cat Items 1 
Main Cat Items 1 -> Categories 1 --> Sub Cat Items 2 
Main Cat Items 1 -> Categories 2 --> Sub Cat Items 3 
Main Cat Items 1 -> Categories 3 --> Sub Cat Items 4 
Main Cat Items 2 -> Categories 4 --> Sub Cat Items 5 

我的查詢無法精煉以獲取結果。所以我必須通過php來完成。這是我試過的。

$input = array_map("unserialize", array_unique(array_map("serialize", $test))); 

使用這個沒有改變我得到了與重複相同的數組。

$serialized = array_map('serialize', $test); 
    $unique = array_unique($serialized); 
    $tdsdsdf = array_intersect_key($test, $unique); 

這一個也是一樣的。

$unique = array(); 
    foreach ($test as $key => $value) { 
     if (!array_key_exists($value['main_cat_id'], $unique)) { 
      $unique[$value['main_cat_id']] = $value; 
     } 
    } 

當試圖這樣做時,它將刪除與所有元素的所有重複項。

這是我剛剛獲得數據庫查詢後的數組。

Array 
(
    [0] => Array 
     (
      [main_cat_id] => 1 
      [main_cat_name] => main cat 1 
      [cat_id] => 1 
      [cat_name] => cat1 parent 1 
      [sub_cat_name] => sub 1 cat1 parent 1 
     ) 

    [1] => Array 
     (
      [main_cat_id] => 1 
      [main_cat_name] => main cat 1 
      [cat_id] => 1 
      [cat_name] => cat1 parent 1 
      [sub_cat_name] => sub 2 cat1 parent 1 
     ) 

    [2] => Array 
     (
      [main_cat_id] => 1 
      [main_cat_name] => main cat 1 
      [cat_id] => 4 
      [cat_name] => cat2 parent 1 
      [sub_cat_name] => sub 3 cat 4 parent 1 
     ) 

    [3] => Array 
     (
      [main_cat_id] => 1 
      [main_cat_name] => main cat 1 
      [cat_id] => 5 
      [cat_name] => cat3 parent 1 
      [sub_cat_name] => 
     ) 

    [4] => Array 
     (
      [main_cat_id] => 2 
      [main_cat_name] => main cat 2 
      [cat_id] => 6 
      [cat_name] => cat 4 parent 2 
      [sub_cat_name] => 
     ) 

    [5] => Array 
     (
      [main_cat_id] => 2 
      [main_cat_name] => main cat 2 
      [cat_id] => 7 
      [cat_name] => cat 5 parent 2 
      [sub_cat_name] => 
     ) 

    [6] => Array 
     (
      [main_cat_id] => 3 
      [main_cat_name] => main cat 3 
      [cat_id] => 8 
      [cat_name] => cat 6 parent 3 
      [sub_cat_name] => 
     ) 

) 
+1

PHP的array_unique – Sekai 2014-11-04 09:38:14

+0

簡單array_unique不起作用。 (無論如何,仍然無法解決) – Yasitha 2014-11-04 09:40:34

+0

仍然無法解決任何幫助? – Yasitha 2014-11-04 09:54:27

回答

1

這是你想要的嗎?

<?php 
$arr = [ 
    ["main_cat_id" => 1, "main_cat_name" => "main cat 1", "cat_id" => 1, "cat_name" => "cat1 parent 1", "sub_cat_name" => "sub 1 cat1 parent1"], 
    ["main_cat_id" => 1, "main_cat_name" => "main cat 1", "cat_id" => 1, "cat_name" => "cat1 parent 1", "sub_cat_name" => "sub 2 cat1 parent1"], 
    ["main_cat_id" => 1, "main_cat_name" => "main cat 1", "cat_id" => 4, "cat_name" => "cat1 parent 1", "sub_cat_name" => "sub 3 cat4 parent1"], 
    ["main_cat_id" => 1, "main_cat_name" => "main cat 1", "cat_id" => 5, "cat_name" => "cat3 parent 1", "sub_cat_name" => ""], 
    ["main_cat_id" => 2, "main_cat_name" => "main cat 2", "cat_id" => 6, "cat_name" => "cat3 parent 1", "sub_cat_name" => ""], 
    ["main_cat_id" => 2, "main_cat_name" => "main cat 2", "cat_id" => 7, "cat_name" => "cat3 parent 1", "sub_cat_name" => ""], 
    ["main_cat_id" => 3, "main_cat_name" => "main cat 3", "cat_id" => 8, "cat_name" => "cat3 parent 1", "sub_cat_name" => ""], 
]; 
$unique = []; 
foreach ($arr as $key => $value) { 
    $unique[$value['main_cat_id']][] = $value; 
} 

function print_me($arr) 
{ 
    foreach ($arr as $main_cat) { 
     echo $main_cat[0]["main_cat_name"] . " -> "; 
     $len = strlen($main_cat[0]["main_cat_name"] . " -> "); 
     foreach ($main_cat as $index => $cat) { 
      echo $cat['cat_name'] . " --> " . $cat['sub_cat_name'] . "\n"; 
      if ($index != count($main_cat)-1) 
       echo str_repeat(" ", $len); 
     } 
     echo "\n"; 
    } 
} 
print_me($unique); 
?> 

很抱歉的str_repeat,我有麻煩的printf ... 輸出:

main cat 1 -> cat1 parent 1 --> sub 1 cat1 parent1 
       cat1 parent 1 --> sub 2 cat1 parent1 
       cat1 parent 1 --> sub 3 cat4 parent1 
       cat3 parent 1 --> 

main cat 2 -> cat3 parent 1 --> 
       cat3 parent 1 --> 

main cat 3 -> cat3 parent 1 --> 
+0

嗯,但是不完全是,我會盡力讓你知道,給我幾分鐘,比你非常多 – Yasitha 2014-11-04 10:06:26

+1

@Yasitha好的,如果你不能做它可以工作,你能給出一個預期的'print_r()'輸出的例子嗎?我不確定你想要什麼作爲鍵。 – Goombi 2014-11-04 10:20:44

+0

我認爲這是我所期待的,但仍然無法顯示它作爲我在這個問題的開頭部分提到Husnis的回答也與你的相同 – Yasitha 2014-11-04 10:25:38

0

嘗試用這種:)

/** 
     * remove duplicate based on the given key 
     * @param {array 2d} 
     * @param {mixed} 
     * @return {array 2d} 
     */ 
    function removeDuplicate($array, $key) { 
     $clearArray = array(); 
     foreach($array as $a) { 
      $found_b = FALSE; 
      foreach($clearArray as $ca) { 
       if($ca[$key] == $a[$key]) { 
        $found_b = TRUE; 
        break; 
       } 
      } 
      if(!$found_b) { 
       $clearArray[] = $a; 
      } 
     } 
     return $clearArray; 
    } 
1

重建陣列:

$result = array(); 
foreach($test as $key=>$val){ 
    $result[$val['main_cat_id']][$val['cat_id']][] = $val; 
} 

顯示類似於fo你提到的rmat:

echo '<pre>'; 
foreach($result as $key=>$main_cat) { 
    echo 'Main Cat Items ' . $key . ' --> '; 
    foreach($main_cat as $key2=>$cat) { 
     foreach($cat as $key3=>$sub) { 
      echo 'Categories ' . $key2 . ' --> '; 
      echo 'Sub Cat Items ' . $key3; 
      echo "\r\n      "; 
     } 
    } 
    echo "\r\n"; 
} 
echo '</pre>'; 
+0

yes這是可以的,但我無法顯示,因爲我想要的(正如我在這個問題的分檔中所展示的)任何想法? – Yasitha 2014-11-04 10:20:49

+1

@Yasitha:你的意思是你需要顯示數組a s以上面提到的格式? – Husni 2014-11-04 10:34:53

+0

是的。我正嘗試用這個數組創建一個菜單。 – Yasitha 2014-11-04 10:37:17