1
\$\begingroup\$

I have a function that returns an array and I want to check that a certain value is not set in the array.

function testFunction() {
    return array('key' => 404)
}

if (testFunction()['key'] < 404) {
    // stay calm and carry on
}
else {
    // uh oh
}

I understand that the above will not work in php 5.3 (or anything below), however, I'd like to know what I should do in the above case. That is - what should I call the temporary array I create for the if statement? Or is there another way of dealing with this situation?

The following is what I would roughly do currently:

function testFunction() {
    return array('key' => 404)
}

$test_array = testFunction();

if ($test_array['key'] < 404) {
    // stay calm and carry on
}
else {
    // uh oh
}

Is this an acceptable thing to do?

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

So, .. you want to simply check whether a certain key in an array is set to a certain value.

You can't do this directly on the function call because older PHP versions don't support array dereferencing. You are right when saying you'll need an intermediate variable.

The only question remaining is what to name it. Considering that the variable will probably be situated in a really small scope, don't worry about it too much. There is a saying which says, the bigger the scope, the bigger the name. You could just name it $list.

Old replies:

Your code does something else as what you describe? If you want to see whether a given value is present in the array, use in_array().

UPDATE:

I believe I understand what you are trying to accomplish now. You just want to see whether the key is set?

array_key_exists()

PHP has got a pretty good online API, be sure to check it for existing implementations when looking for a solution.

\$\endgroup\$
4
  • \$\begingroup\$ According to the code, Jonathan also wants to compare against the value mapped to that key if there is one. \$\endgroup\$ Mar 3, 2011 at 2:44
  • \$\begingroup\$ I want to know if a specific key has the value 404. so in_array() will not work because the value may exist somewhere else and array_key_exist will not work because I know the key exists. I want to know if a specific key I know is there has the value 404. \$\endgroup\$ Mar 3, 2011 at 2:56
  • \$\begingroup\$ @Jonathan Mayhak: K, I finally understand your question now. ;p I updated the answer. \$\endgroup\$ Mar 3, 2011 at 3:21
  • \$\begingroup\$ I was just curious if there was some standard in php for dealing with this situation. Good to know what I've been doing is acceptable. \$\endgroup\$ Mar 3, 2011 at 3:36
1
\$\begingroup\$

Your question does not match your code. You ask to check that a key does not exist in the array, yet you are actually testing the value mapped to that key against some other value, presumably doing something else if the key doesn't exist.

$result = testFunction();
if (!isset($result['key']) || $result['key'] < 404) {
    // ok
}
else {
    // panic
}

You can create a helper method with a suggestive name that does the full check and use that in your if clause.

function omgWeGotTheDreaded404() {
    $result = testFunction();
    return isset($result['key'] && $result['key'] >= 404;
}

...

if (omgWeGotTheDreaded404()) ...
\$\endgroup\$
2
  • \$\begingroup\$ Your answer is the same thing I wrote above for the second example. I know I can do what you wrote. I want to know if what we both wrote is the best way. What do you think is a good thing to name the temporary array? (you used $result and I used $test_array) \$\endgroup\$ Mar 3, 2011 at 2:59
  • \$\begingroup\$ Steven's answer about the name is good advice. It lives such a short life, give it a terse name. My point is that I'd still put it inside a function that returns bool so you don't need a temporary array outside the function. If you only do this in one place, it's not of much value. \$\endgroup\$ Mar 3, 2011 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.