How can PHP be used to display search results based on percentage matches in different arrays?
To display search results based on percentage matches in different arrays, you can use PHP to compare the similarity between the search query and the elements in the arrays. One way to achieve this is by using functions like similar_text() or levenshtein() to calculate the similarity percentage. Then, you can sort the results based on the percentage match and display them accordingly.
<?php
// Sample arrays
$array1 = ["apple", "banana", "orange", "pear"];
$array2 = ["apples", "mango", "peach", "pear"];
$searchQuery = "apple";
$matches = [];
foreach ($array1 as $item1) {
similar_text($searchQuery, $item1, $percentage);
$matches[$item1] = $percentage;
}
foreach ($array2 as $item2) {
similar_text($searchQuery, $item2, $percentage);
$matches[$item2] = $percentage;
}
arsort($matches); // Sort by similarity percentage
foreach ($matches as $item => $percentage) {
echo "$item - $percentage% match <br>";
}
?>