C
C
CrewCut2016-06-20 13:59:24
PHP
CrewCut, 2016-06-20 13:59:24

How to remove implicit duplicates in php array?

There is an array of strings, some of which can be implicit duplicates of each other, for example: "aspen leaf" and "aspen leaf". Those. the same words, but in a different order. The task is to remove such implicit duplicates. Now I'm thinking of doing:
1. Create an associated array, where there will be a "string" => "string, where the words are sorted alphabetically"
2. Remove duplicates by the values ​​of the associated array
3. Create a simple array from the keys of the cleared array.
Actually, two questions - can it be done easier? If not, then how to remove duplicates by key/value in the associated array? I only know array_unique, but it is not suitable for this.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-06-20
@CrewCut

$strings = ['лист осиновый', 'осиновый лист'];

$result = array_unique(array_map(function($str) {
    $arr = mb_split('\s', $word);
    sort($arr);

    return implode(' ', $arr);
}, $strings));

but this is not the final version, of course, as the word order changes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question