find word inside array strings

Asked

Viewed 512 times

1

I’m using the following code:

foreach ($lista_grupos as $key => $value) {
    if(strpos($lista_grupos[$key],'Tecnologia_da_Informacao')===true){
        $admin = true;
    }else{
        $admin = false;
    }
}

But it always returns false, it doesn’t find the string I want. What am I doing wrong? Follows array:

(array =>
    [21] => CN=Tecnologia_da_Informacao,OU=Tecnologia_da_Informacao,OU=03-Operadora,DC=asdt,DC=com,DC=br
    [22] => CN=asdasd,OU=02-Grupos,DC=uniiaca,DC=com,DC=br
    [23] => CN=asdasd,OU=02-Grupos,DC=jçasdia,DC=com,DC=br
)
  • That looks like Idap to me.

  • It is, but I haven’t figured out how to get exactly the name of the group, this way is bringing the groups that the person participates in an array

  • You need to quit the loop when you find what you’re looking for, or you can change your variable to false in the next step.

  • I did the test, but does not enter the true if.

  • if(strpos($value, 'Tecnologia_da_Informacao') !== false)

  • I didn’t understand, why other than false?

  • 1

    Dude, slap that php code

Show 2 more comments

1 answer

1


It’s been a long time since I programmed in php but for what I read in documentation, the strpos() returns an integer and you compare it to Boolean there. By the documentation, use something like this:

if ($pos !== false) {
    $admin = true;
    return ...;
}

The === compares the result plus the type. O strpos() can return false but will never return true and yes an integer of the first occurrence of your string (PHP operators).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.