Answer the question
In order to leave comments, you need to log in
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
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] => подвесная
)
*/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question