0

I have this code here:

if (in_array('mystring', $entry->getCategories()->getValues()))
{
   ... //do somethingA

This works.

However, I want to allow somethingA to run, if mystring OR mYstring or MYSTRING is this case.

So I've tried like so:

if (in_array(array('OCC', 'OCc','Occ', 'occ', 'ocC','oCC', 'oCc', 'OcC'), $entry->getCategories()->getValues()))
{

But I get nothing returned. What am I missing here?

2
  • Well, what does getValues() return? Commented Dec 6, 2010 at 18:10
  • @Pekka - it's a string as expected so I guess. (true, I haven't dump them, I just thought that the problem will be on this in_array miss use. and apparently it was. Lucky guess, and I should have dump it more. Commented Dec 6, 2010 at 18:20

3 Answers 3

5

in_array() cannot compare the values of two arrays, nor can it compare strings case-insensitively. From the manual:

If needle is a string, the comparison is done in a case-sensitive manner.

You can try this trick instead. Map strtolower() to the categories array so everything is in lowercase, then search the array for the lowercase string:

$cats_lower = array_map('strtolower', $entry->getCategories()->getValues());

if (in_array('mystring', $cats_lower))
{
    // Do something
}
Sign up to request clarification or add additional context in comments.

1 Comment

Let me forever be known as the guy who put you over 20k. Nice answer by the way. xD
1

Something which is more future proof (try including all possible combinations of long strings), try:

if (in_array(strtolower('myStrIng'), array_map(strtolower, $entry->getCategories()->getValues()))) { 
    // do something
}

Comments

0
foreach($entry->getCategories()->getValues() as $val) {
  if(preg_match("/$val/i", "occ") {
    somethingA();
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.