S
S
Sergey2019-08-14 14:38:32
PHP
Sergey, 2019-08-14 14:38:32

How to remove all single root words from an array?

How to remove all single-root words from an array, leaving one element from such similarities?
An example of an array:
[47] => panel
[48] => pencil case
[49] => overflow
[50] => suspended
[51] => suspended
[52] => suspended
[53] => suspended
We see that the same root for the following elements:
[50] => suspended
[51] => suspended
[52] => suspended
[53] => suspended
Only the first such element should be left in the array, the rest should be deleted.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lander, 2019-08-14
@oldzas

function clearBySixFirstLetter($array) 
{
    $has = [];
    
    return array_filter(
        $array,
        function ($word) use (&$has) {
            $sixLetters = mb_substr($word, 0, 6);
            
            if (!in_array($sixLetters, $has)) {
                array_push($has, $sixLetters);
                return true;
            }
            
            return false;
        }
    );
}

clearBySixFirstLetter([
    'панель',
    'пенал',
    'перелив',
    'подвесная',
    'подвесного',
    'подвесное',
    'подвесной',   
]);

/*
Array
(
    [0] => панель
    [1] => пенал
    [2] => перелив
    [3] => подвесная
)
*/

D
Developer, 2019-08-14
@samodum

Actually, the root in these words is "weight".
School program.
Redo the question

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question