G
G
Gravio2018-12-25 20:13:17
PHP
Gravio, 2018-12-25 20:13:17

How to search for words in multiple arrays at once?

I have an array with text for example: And two arrays with words
$text = "зеленый красный желтый"

$color_a = array("зеленый","желтый"); 
$color_b = array("красный","черный");

How can I search for words in two arrays at once, and how to find out the number of matches in each array
My example, but it seems to me wrong + I don’t know how to count the number of matches for each array:
$text = @explode(" ",$text);
foreach($text as $word){
   foreach ($color_a as $aword) {
      if($word == $aword)
      {
          echo 'a++';
      }
    }
    foreach ($color_b as $bword) {
       if($word == $bword)
       {
           echo 'b++';
       }
     }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2018-12-25
@Gravio

$text = explode(' ', $text);

$intersect_color_a = array_intersect($color_a, $text);
$intersect_color_b = array_intersect($color_b, $text);

$count_a = count($intersect_color_a);
$count_b = count($intersect_color_b);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question