Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

php - Remove duplicate value in multidimensional array

all I want to remove the duplicate value from this Array

Array
(
    [0] => Array
        (
            [0] => Ajay Patel
            [1] => Tag 1
        )

    [1] => Array
        (
            [0] => Tag 1
            [1] => Tag 3
        )

    [2] => Array
        (
        )

    [3] => Array
        (
        )

    [4] => Array
        (
        )

)

I tried this solution from How to remove duplicate values from a multi-dimensional array in PHP

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

But i think something is wrong here, i am getting this as result.

Array
(
    [0] => Array
        (
            [0] => Ajay Patel
            [1] => Tag 1
        )

    [1] => Array
        (
            [0] => Tag 1
            [1] => Tag 3
        )

    [2] => Array
        (
        )

)

What i want is

Array
            (
                [0] => Ajay Patel
                [1] => Tag 1
                [2] => Tag 3
            )

Tag 1 is removed because its 2 times...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try this

$result = array();
function merge_values(array &$array, $mixed) {
    if(is_array($mixed)) {
        foreach($mixed as $tags) {
            merge_values($array, $tags);
        }
    }
    else {
        if(null !== $mixed && strlen($mixed) > 0 && false === array_search($mixed, $array)) {
            $array[] = $mixed;
        }
    }
}

merge_values($result, $array);
print_r($result);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...