Answer the question
In order to leave comments, you need to log in
PHP ARRAY LOOKING - Nested IF Conditions -?
Help - the whole brain broke how to nest a condition in an array iteration, or OUTSIDE it,
but so that there are no false positives if , unnecessary iterations of options.
let's say an array
$arr["TOASTER"] = "APPLIANCE";
$arr["MILK"] = "PRODUCT";
$arr["SHIRTS"] = "CLOTHES";
tell me a couple of options, more intelligently - iterate over the array
if there are matches = give the answer
if there are no matches = write
foreach($arr as $key => $value)
{
if($key == "$message") { insert $value }
else { echo" NO MATCH"; // how many elements of the array - so many times will be displayed ; }
}
Answer the question
In order to leave comments, you need to log in
but no false positives
If you want similarity , and not just exact matches, then this is a difficult task. There are many algorithms for calculating similarity , for every taste and color. Depending on your conditions, you can take any or even invent your own.
<?php
$arr=[];
$arr["ТОСТЕР"] = "ТЕХНИКА";
$arr["МОЛОКО"] = "ПРОДУКТ";
$arr["РУБАШКА"] = "ОДЕЖДАТ";
$text_to_find = 'рубаш';
$maximum = 0; //Процент совпадения
$answer = ''; //Ответ
foreach($arr as $key => $value)
{
similar_text(mb_strtolower($key), mb_strtolower($text_to_find), $perc);
if ($perc > $maximum) {
$maximum = $perc;
$answer = $value;
}
}
if ($maximum > 70) { //Если совпадение хотя бы 70%
echo $answer." с шансом $maximum%";
} else { //Иначе считается, что ничего не нашли
echo("Нет совпадений! Но могло бы быть $answer с шансом $maximum%");
}
?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question