0

I find myself using array_key_exists() a disturbing amount of times. With a huge number of

if(array_key_exists($key, $array)){
    // do stuff with $array[$key];
}

interspersed with

$value = array_key_exists($key, $array)? $array[$key] : "";

Maybe I've developed poor habits by other languages letting you check whether a key exists in an array just by trying to access it.

But the code I'm writing feels super clunky and verbose when array_key_exists() is required most times I'm trying to access an array of data that's not handwritten by me.

Any tips? Maybe I should think about array differently in php? Or are there any modern methods that can help accessing arrays in a more streamlined fashion?

4
  • You could maybe use isset() instead? It behaves slightly differently but it's less verbose. Docs here: php.net/manual/en/function.isset.php Commented Dec 4, 2021 at 7:13
  • @purple Hmm. Is it less verbose because it's fewer characters or can you use it in a smarter manner than I am using array_key_exists at the moment? Commented Dec 4, 2021 at 7:16
  • Hmmm mainly fewer characters/nicer syntax (imo). However, isset() returns false if a key exists but is null whereas array_key_exists() would return true. So if you care about checking for null values, isset() could be more concise in that regard. Commented Dec 4, 2021 at 7:27
  • @purple Ah! Returning false is pretty big under some circumstances! That would help make some things neater :) Commented Dec 4, 2021 at 7:31

2 Answers 2

3

You can use null coalescing operator ?? in PHP >= 7.0

$array = ['fruit' => 'apple', 'tree' => 'oak'];

echo $array['vegetable'] ?? 'default_value'; 
Sign up to request clarification or add additional context in comments.

1 Comment

0

Without knowing your "super clunky" code, one can't really answer your question. But in general one can say that the frequent query for an array_key is nothing reprehensible.

But maybe you could take the approach of checking the individual keys at the beginning of your script or programme. And the setting of the keys is a condition, otherwise they are set with a default value (for example with the ?? operator). This saves you the query in the code about the existence of the key.

But as I said, you would have to see the code. but maybe it helps a little.

2 Comments

I get that sentiment. It was pretty hard to come up with multiple succinct examples of an array maybe having a value, and demonstrating why it might not have. In such a way that people wouldn't start fixing the examples over addressing the question of avoiding verbosity. My question, at the end of the day, is probably just "is this a piece of clunky verbosity that you have to live with whenever accessing arrays in php" :P
@Fluxian I see. There is a lot to be said for optimising your programme architecture.

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.