V
V
Vladimir NEO2019-04-08 11:56:54
Arrays
Vladimir NEO, 2019-04-08 11:56:54

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

2 answer(s)
M
Maxim Timofeev, 2019-04-08
@SW-EN

but no false positives

what should they be? Where is your poorly performing code that needs to be optimized?
You are asking about optimizing conditions, but you are asking for overkill. You decide what you need. Wangyu:
https://www.php.net/manual/ru/function.in-array.php
or
https://www.php.net/manual/ru/control-structures.s...
what with what? Where do we write down what it means to "give an answer"?

D
dollar, 2019-04-09
@dollar

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.

A simple example using the similar_text function
<?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%");
}
?>

But there are other functions, such as levenshtein() , which calculates the Levenshtein distance, and so on.
PS Please mark good answers as solutions. People are trying to write for you. This applies to previous questions too.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question