如何从多维数组中删除一些重复的元素

时间:2014-11-04 09:36:11

标签: php arrays multidimensional-array

我会提到我的阵列。我需要从数组中删除重复键,但我还需要保持其他元素合并。我知道这不够清楚,所以我会解释。

这是我想要的预期结果。

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;
        }
    }

尝试使用它时,它会删除包含所有元素的所有重复项。

这是我在db查询之后得到的数组。

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] => 
        )

)

3 个答案:

答案 0 :(得分: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 --> 

答案 1 :(得分:1)

重建阵列:

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

显示您提到的格式:

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

答案 2 :(得分: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;
    }