Shahrukh
Shahrukh

Reputation: 45

PHP - How to Remove Duplicate Keys in Multidimensional Array

I have a multidimensional Array I want to delete all duplicate keys, I've tried but not working any script.

Following answer is working for duplicate values and I want same solution for duplicate keys in multidimensional array.

PHP remove duplicate values from multidimensional array

$arr = array(
    "a" => "xxx",
    "b" => "xxx",
    "c" => array(
        "g" => "xxx",
        "a" => "xxx",
        "h" => "xxx",
    ),
    "d" => "xxx",
    "e" => array(
        "i" => array(
            "a" => "xxx",
            "k" => array(
                "l" => "xxx",
                "b" => "xxx",
            ),
        ),
        "j" => "xxx",
    ),
    "f" => "xxx",
);

I want this result:

$arr = array(
    "a" => "xxx",
    "b" => "xxx",
    "c" => array(
        "g" => "xxx",
        "h" => "xxx",
    ),
    "d" => "xxx",
    "e" => array(
        "i" => array(
            "k" => array(
                "l" => "xxx",
            ),
        ),
        "j" => "xxx",
    ),
    "f" => "xxx",
);

I'm trying to solve this issue by this function:

function array_unique_key($array) { 
    $result = array(); 
    foreach (array_unique(array_keys($array)) as $tvalue) {
        if (is_array($tvalue)) {
            $result[$tvalue] = $array[$tvalue]; 
        }
        $result[$tvalue] = $array[$tvalue]; 
    } 
    return $result; 
} 

Upvotes: 0

Views: 972

Answers (2)

Nick
Nick

Reputation: 147146

This function will do what you want. It recursively traverses the array, looking for keys which have already been encountered and where it finds them, unsetting that element from the array. Note we pass $array and $keys by reference to the function so they can be changed (thanks @Don'tPanic for pointing out the need for $keys being by reference as well):

function remove_dup_keys(&$array, &$keys = array()) {
    foreach ($array as $key => &$value) {
        if (in_array($key, $keys)) {
            unset($array[$key]);
        }
        else {
            $keys[] = $key;
        }
    }
    foreach ($array as $key => &$value) {
        if (is_array($value)) {
            remove_dup_keys($value, $keys);
        }
    }
}

Output:

Array (
    [a] => xxx
    [b] => xxx
    [c] => Array (
        [g] => xxx
        [h] => xxx
    )
    [d] => xxx
    [e] => Array (
        [i] => Array (
             [k] => Array (
                 [l] => xxx
             )
        )
        [j] => xxx
    )
    [f] => xxx 
)

Demo on 3v4l.org

Upvotes: 2

Don't Panic
Don't Panic

Reputation: 41810

Another option if you want to keep the original array.

function array_unique_key($input, &$keys = []) {

    // filter any preexisting keys from the input while adding its keys to the set of unique keys

    $input = array_filter($input, function($key) use (&$keys) {
        return isset($keys[$key]) ? false: $keys[$key] = true;
    }, ARRAY_FILTER_USE_KEY);

    // recursively map this function over the remaining values

    return array_map(function($value) use (&$keys) {
        return is_array($value) ? array_unique_key($value, $keys): $value;
    }, $input);

}

Upvotes: 2

Related Questions