5

Is there a short way of doing this?

if ((isset($a['key']) && ($a['key'] == 'value')) {
  echo 'equal';
  // more code
}
else {
  echo 'not equal';
  // more code
}

I need to test lots of values on an array that can or cannot exist. I feel that this method is too verbose.

I could remove the isset() and mute the notices... but then I feel dirty.

Edit:

Answering Jack's question: "Could you give an example how you would test lots of values in an array?"

example:

if (isset($_GET['action']) && $_GET['action'] == 'view') {
  //code
}
if (isset($_GET['filter']) && $_GET['filter'] == 'name') {
  //code
}
if (isset($_GET['sort']) && $_GET['sort'] == 'up') {
  //code
}
if (isset($_GET['tag']) && $_GET['tag'] == 'sometag') {
  //code
}
etc...
Peter
  • 5,138
  • 5
  • 29
  • 38
  • Imagine that there's more code inside. What I want to make shorter is the part inside the "if ()". – Peter Nov 12 '12 at 18:06
  • 2
    The only shorter expression is one without the redundant parentheses :) – Ja͢ck Nov 12 '12 at 18:06
  • it is not that long or unreadable. this micro optimisation will lead you no where. – itachi Nov 12 '12 at 18:07
  • As it stands now, you shouldn't receive warnings from the interpreter (provided you remove that stray `(`). Are you? – Jason McCreary Nov 12 '12 at 18:10
  • Could you give an example how you would test lots of values in an array? – Ja͢ck Nov 12 '12 at 18:34
  • The best approach is to use a function. I don't see a reason not to use a function like "function isEqual($array, $key, $value){ //your condition}" or better practice in OOP to have a class that contains the array and that way you don't have to pass the array each time – PHP lover Nov 12 '12 at 18:36
  • Thanks for the example, updated my answer with something else to consider. – Ja͢ck Nov 12 '12 at 18:48
  • You can do that by a foreach loop. Would you like to have an insight of that ? Let me know if you are. I will write a demo for you. – MD. Sahib Bin Mahboob Nov 12 '12 at 18:51

4 Answers4

8

For anyone still stumbling upon this question...

You could use PHP's coalescing operator:

if (($a['key'] ?? '') === 'value') {
  echo 'equal';
  // more code
}
else {
  echo 'not equal';
  // more code
}

See this question: using PHP's null coalescing operator on an array

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Fabien Snauwaert
  • 4,995
  • 5
  • 52
  • 70
1

I don't like to answer my own questions but I feel that the best and cleaner way to do this kind of checkings is to write a "helper funcion" like:

function iskeyval(&$a, $k, $v) {
  return isset($a['key']) && ($a['key'] == 'value');
}

and then:

if (iskeyval($a, 'key', 'value')) {
  ...
}
else {
  ...
}
Peter
  • 5,138
  • 5
  • 29
  • 38
  • 2
    You can always write a function around something, but afaict the question was whether the expression could be made shorter and it can't :) btw, you can use `(array $a, $k, $v)` as the argument list instead to let PHP check the argument type for you. – Ja͢ck Nov 14 '12 at 15:16
0

I have added comments to explain the code. Here is the code :

//this array maps the function with the get parameters
$functions = array (
"action" => "do_actions" ,
"filter" => "do_filters"
);


foreach ($_GET as $key=>$value) {
    //check if this field is corresponding functions or not  
    if ( array_key_exists($key , $functions) ) {
        call_user_func($functions[$key] , $key,$value);
    }
}


function do_actions ($key , $value) {
    //place your code here to play with this value
    echo 'do_actions is called with ' . $key . 'and' . $value . "</br>";
}

function do_filters ($key , $value) {
    //place your code here to play with this value
    echo 'do_filters is called with ' . $key . ' and ' . $value .  "</br>";
}


?>
MD. Sahib Bin Mahboob
  • 20,246
  • 2
  • 23
  • 45
-2
$list = array(
0 => 'one',
1 => 'two',
2 => 'one',
3 => 'three',
4 => 'one',
);

if( @$list['xxx'] !== 'three')
    echo 'Not ';


echo 'Equal';

Suppress the error reporting.

Cups
  • 6,901
  • 3
  • 26
  • 30