Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...