Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Say I have a data structure like this:

array(3) {
  ["id"]=>
  int(1)
  ["name"]=>
  string(3) "foo"
  ["message"]=>
  array(1) {
    ["action"]=>
    string(3) "PUT"
  }
}

Where 'message' and 'action' are both optional. To check if they're present my first attempt would be to write something like this:

if (array_key_exists('message', $array) && array_key_exists('action', $array['message'])){
}

Is there a cleaner implementation?

share|improve this question
    
See also: stackoverflow.com/questions/2948948/… You can write an array_key_exists utility function which works recursively. –  Connor McArthur Mar 19 '13 at 0:40

3 Answers 3

up vote 1 down vote accepted

No, this is just the way of doing it.

Even when I'm not limited to a certain script language, I can't think of an cleaner solution?

share|improve this answer

Not clearer but probably the fastest solution:

if ((isset($array["message"]["action"]) ||
        (array_key_exists('message', $array) && array_key_exists('action', $array['message']))) {
    /* ... */
}

The second check is needed because some values evaluated with isset() will result false despite the index does exist.

share|improve this answer

You can just do this!

if (isset($array["message"]["action"])) { /*...*/ }

Proof:

<?php

error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);

$array = [
    "id" => 1,
    "name" => "foo",
    "message" => [
        "action" => "PUT",
    ],
];

var_dump(isset($array["message"]["action"])); // true


$array = [
    "id" => 1,
    "name" => "foo"
];

var_dump(isset($array["message"]["action"])); // false

No log entries.

Do note that if your value is null, then isset will also return false. You probably shouldn't be creating values with null.

share|improve this answer
    
This answer is also missing a ) and may cause a log entry to be generated due to not checking isset($array['message']) before dereferencing it. –  Brythan Nov 1 '14 at 23:13
    
@Brythan no it does not cause a log entry. But yes it is missing a ). –  CMCDragonkai Nov 2 '14 at 5:10
    
Sorry, I meant to point out that you'd have to crank your error reporting. It absolutely causes a log entry if you have your error reporting set for it. I've had it spam my logs with that previously. This was the default logging at one point. It may not be the default now (or your host may have dialed down error reporting), but I am absolutely sure that that situation can generate a log entry under certain configurations. –  Brythan Nov 2 '14 at 5:21
    
No it really doesn't I tested it with all error reporting switched on! –  CMCDragonkai Nov 2 '14 at 5:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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