1

I have this code:

return in_array( get_theme_mod( 'header_style' ), ['style-1','style-2','style-3','style-4', 'style-6'] );

Which should return whether or not header_style (which could be style-1, style-2, etc.) is in that list precisely.

In simpler terms, it's:

in_array( 'style-1', ['style-1','style-2','style-3','style-4', 'style-6'] );

How could I re-write this with an isset?

coolpasta
  • 725
  • 5
  • 19
  • Ok, I'll bite: Why would you want to rewrite this with an 'isset'? – KIKO Software Jul 11 '18 at 10:31
  • 1
    isset checks for the existence of an array _key_, not a value. So you would have to rewrite your array to use the values as the keys (with pseudo values), and that makes rather little sense. – CBroe Jul 11 '18 at 10:31
  • @KIKOSoftware Out of the scope to answer that, but I have a lot of these functions where I check for these values, I'm fearful that as I add more values (hundreds), it'll start to be a hog. – coolpasta Jul 11 '18 at 10:32
  • 3
    Possible duplicate of [what is faster: in\_array or isset?](https://stackoverflow.com/questions/13483219/what-is-faster-in-array-or-isset) – CBroe Jul 11 '18 at 10:32
  • @CBroe It's not a duplicate. That question is about which is faster; this question is about how to actually utilize the knowledge that isset is faster in most circumstances. PHP has tools for dealing with this exact scenario (e.g., array_flip). – Zenexer Jul 11 '18 at 17:12
  • @Zenexer that was mostly based on the comment, _"but I have a lot of these functions where I check for these values, I'm fearful that as I add more values (hundreds), it'll start to be a hog."_ Ability to research how to flip an array I rather assumed as a given in this context ... – CBroe Jul 11 '18 at 18:38

2 Answers2

1

isset checks if value of a key is set, so you need to rebuild your second array as (just an example):

// `style-` become keys
['style-1' => true, 'style-2' => true, 'style-3' => true, 'style-4' => true, 'style-6' => true];

Now, you can use isset:

$allowed_styles = ['style-1' => true, 'style-2' => true, 'style-3' => true, 'style-4' => true, 'style-6' => true];
// option is:
// $allowed_styles = array_fill_keys(['style-1', 'style-2'], true);

return isset($allowed_styles[get_theme_mod( 'header_style' )]);
u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

As explained in another question, isset is, in fact, faster than in_array. in_array is O(n), whereas isset is O(1).

One way to use isset is to rewrite your array in the form:

$array = ['style-1' => true, 'style-2' => true, 'style-3' => true, 'style-4' => true, 'style-6' => true];

If you're only searching an array once, that's the best way to go about this. However, if you dislike the verbosity of that method, and you plan to check for the existence of an element multiple times, you could do this instead:

$array = ['style-1', 'style-2', 'style-3','style-4', 'style-6'];
$array = array_flip($array);

This will reverse the keys and values, so $array['style-1'] will be 0, $array['style-2'] will be 1, and so on. You can then either use isset or array_key_exists:

return array_key_exists( get_theme_mod( 'header_style' ), $array );

I've heard rumors that array_key_exists is faster than isset for this purpose as of PHP7, but I haven't actually looked at any benchmarks.

Zenexer
  • 18,788
  • 9
  • 71
  • 77