There was such a question. I am writing a bot assistant for the site and I need to analyze the phrases of visitors, but there can be a great many variations, so the question arose, is it possible to put an array instead of a single value in the case and compare it with this array of answers? And if at least one value matches, execute this case
|
1 answer
Can. But it looks like the wrong approach. Finding a match in an array is quite a costly operation, and you suggest searching in several arrays. If you approach the issue purely academically, the code will look like this:
$var = 'Hello'; $var = strtolower($var); $array1 = array('hello', 'hi', 'yo'); $array2 = array('what', 'where', 'when'); switch (true) { case in_array($var, $array1): //do something break; case in_array($var, $array2): //do something else break; } - Can you suggest a way to do this faster and less expensive? - NTP
- @ So-so, I am not familiar with your subject area, but it seems to me a more appropriate option is to use a lightweight database, where the search will be based on a prepared phrase (strtolower, htmlspecialchars and the like). If you reduce all the problems to the keys, you can use NoSQL solutions, if not, then MySQL. - Dmitry
|
switchquite clearly. Give me some sample code, and maybe they will tell you how to optimize its spelling - teran