0

So you have the option to structure an array as you please knowing that a few lines further down in your code you are going to need to check for the existence of a value in that array. The way I see it you have at least two options:

$values_array = array(
    'my_val',
    'my_val2',
    'and_so_on',
);

if(in_array('my_val', $values_array)) {
    var_dump('Its there!');
}

Or you could use an associative array and use the keys to contain your value:

$values_array = array(
    'my_val'    => '',
    'my_val2'   => '',
    'and_so_on' => '',
);

if(isset($values_array['my_val'])) {
    var_dump('Its there!');
}

Which method would you pick and why? Would you be solely aiming to reduce process time or also minimise the amount of memory used?

Perhaps you wouldn't use my two puny methods and have another awesome way to solve this simple problem.

This is a theoretical question with no real world application in mind, but there could be thousands of options in the array. It is a speculative question really to see which method is considered better by everyone. Whether it be considered so for readability, speed or memory usage reasons.

Treffynnon
  • 21,365
  • 6
  • 65
  • 98
  • If you're using a map (as in your second example), consider `array_key_exists` instead of `isset` - else a E_NOTICE is thrown for every nonexistent key you try to access. – Piskvor left the building Apr 07 '11 at 13:00
  • @Piskvor True, but many have benchmarked the two and found `isset()` to be faster. For example: http://www.cybersprocket.com/2010/programming-languages/array_key_exists-versus-isset/ and more interestingly http://stackoverflow.com/questions/700227/whats-quicker-and-better-to-determine-if-an-array-key-exists-in-php – Treffynnon Apr 07 '11 at 13:05
  • 2
    @Treffynnon: If you're worried about `isset` vs. `array_key_exists` performance, you're doing premature optimization. That can be fun, but if you keep spending significant time on it, it's an indication of problems (not to mention that the performance problem is probably elsewhere than in the compiled, built-in functions). – Piskvor left the building Apr 07 '11 at 13:22
  • @Piskvor +1 for premature optimisation and pointing out there are easier fish to fry or low hanging fruit elsewhere in the code that are more likely to affect the final outcome. – Treffynnon Apr 07 '11 at 13:25
  • @Piskvor isset throws me no error for the undefined key. What I am doing wrong? – Your Common Sense Apr 07 '11 at 13:27
  • @Col. Shrapnel: Possibly you have the default error reporting set: `E_ALL ^ E_NOTICE`. By setting to `E_ALL`, you should be seeing the notices, too. (also, it's a notice, so it doesn't stop script execution, but it clutters up the logs and/or looks ugly) – Piskvor left the building Apr 07 '11 at 13:39

3 Answers3

5

Really? Use the variant, that fits better to your and your applications needs. Especially with so less elements, it is far out of scope of measurement.

But there is a real semantic difference between both. The first one defines a list, the second one defines a map. If $array should represent a list, use the first one, if it should represent a map, use the second one (obvious, huh? ;)).

At all: Let never such micro optimization approaches influence your application design.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • It is a theoretical question with no real world application in mind, but there could be thousands of options in the array. It is a speculative question really to see which method is considered better by everyone. Whether it be considered so for readability, speed or memory usage reasons. – Treffynnon Apr 07 '11 at 12:59
  • 1
    Agreed that allowing it to influence the application design is not a good idea. I am merely curious to see peoples responses. – Treffynnon Apr 07 '11 at 13:01
3

Code readability and maintainability always trump optimisation.

The developer-time wasted trying to untangle code that is deliberately obtuse just to save a few microseconds generally outweights the value of those saved microseconds.

If you're writing something where execution speed really makes enough of a difference to care about this sort of thing, then PHP (or indeed any interpreted language) is probably the wrong language. And if you are trying to optimise PHP code, there's almost certainly better places to start than this.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • I tend to agree and regularly chastise the developers on my team who have come from Ruby (RoR) for sacrificing readability! – Treffynnon Apr 07 '11 at 13:27
1

Well, I'd tend to the second one, as I have a feeling that this one is more optimal. And I am using it quite often.
However, if it become a bottleneck, I'd measure alternatives.

Anyway, I would never think of these thing while my arrays being relatively small - up to several hundreds items. And I would try to avoid such searches on heavy arrays at any cost anyway.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345