0

So I have an multidimensional Array that looks like this:

Array 
( 
 [0] => Array 
   ( 
   [0] => Date 
   [1] => Time 
   [2] => Duration 
   [3] => Info 
   [4] => Client
   )
 [1] => Array 
   ( 
   [0] => 2021-12-01 
   [1] => 10:45:43 
   [2] => 237 
   [3] => Test none
   [4] => Client 1 
   ) 
 [2] => Array 
   ( 
   [0] => 2021-12-01 
   [1] => 11:29:13 
   [2] => 77 
   [3] => Test important
   [4] => Client 2 
   ) 
 [3] => Array 
   ( 
   [0] => 2021-12-01 
   [1] => 11:53:03 
   [2] => 44 
   [3] => Info 
   [4] => Client 1 
   )

and what I need is to count by the values in the sub array. An example, I am using the array_walk_recursive function like this:

array_walk_recursive($dataArray, function ($i) use (&$countRes) {
        $countRes += (int) ($i === 'Info');
      });

And the Output for this is correct i get the number 2. Thats perfect. But what I need now is to count the sub_arrays with the "Test" value. The problem is that in the Text there is always something different after the "Test" value like, "Test none" or "Test important". Regardless of what is after the text "test" how can I still count them?

I have tried with some wildcard symbols but it is not working. Any suggestions how to do it?

array_walk_recursive($dataArray, function ($i) use (&$countReUb) {
        $countReUb += (int) ($i === 'Test*');
        $countReUb += (int) ($i === 'Test.*');
        $countReUb += (int) ($i === 'Test%');
      }); 

None of them are working.

Acelando
  • 29
  • 10
  • 2
    Does this answer your question? [How do I check if a string contains a specific word?](https://stackoverflow.com/questions/4366730/how-do-i-check-if-a-string-contains-a-specific-word) – 0stone0 Jan 21 '22 at 14:58
  • Well it’s always the same at the beginning, so it will always be: 'Test' and then the next word. And the word count does not count if it is in another word like 'Testimony'. So it will always be: 'Test' and then something else that changes. – Acelando Jan 21 '22 at 15:08
  • 1
    `$countRes += (int) (false !== strpos($i, 'Test'));` – u_mulder Jan 21 '22 at 15:14
  • Perfect u_mulder, that’s it. Working perfectly fine – Acelando Jan 23 '22 at 08:22

1 Answers1

0

If I needed to determine whether 'Test' was the first word in a string, you can either do some string manipulation to substring the first 4 characters and check to see if it's === 'Test', or use a Regular Expression.

// THE STRING BEGINS WITH THE WORD 'Test'
$testExpression = '/^Test/';

array_walk_recursive($dataArray, function ($val) use (&$countReUb) {
  
  if(is_string($val))
  {
    $foundMatch = (preg_match($testExpression, $val) === 1);
    if($foundMatch)
    {
      $countReUb++;
    }


    /*

    if(strlen($val) > 3)
    {
      if(substr($val, 0, 4) === 'Test')
      {
        $countReUb++;
      }
    }
    */
  }
  
});

https://www.php.net/manual/en/function.preg-match.php