I'm fairly new to PHP and I have a question.
I'm trying to make a function that adds tags to a text. My function works but the order of the tags in the array returned is wrong.
How can I change the order, please
Thanks for any help.
<?php
$tags = [
'animals' => ['cat', 'dog', 'horse', 'ferret'],
'nature' => ['walk', 'outdoor', 'tree', 'plant']];
function getTags(string $text, array $tags): array
{
$lowerC = strtolower($text);
$str = preg_replace("/[^A-Za-z\'\- ]/", '', $lowerC);
$arrayT = explode(" ", $str);
$tagArray = [];
foreach ($tags as $tag => $value) {
if (array_intersect( $value, $arrayT )) {
$tagArray[] = $tag;
}
} return $tagArray;
}
$res = getTags('During my walk, I met a white horse', $tags);
var_dump($res); // returns ['animals', 'nature'] but I'm trying to get ['nature', 'animals']
$tagsnaturefirst, what is the logic (first encountered word, most words?)