Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

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

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

I'm using this in a project of mine, but I'm using null as a muted state. (php 5.6)

<?php

function &option_select($options, $default, ...$path) {
    if(!isset($path[0])) { return $options; }
    $cursor = &$options;
    foreach($path as $key) {
        if(isset($cursor[$key])) { $cursor = &$cursor[$key]; } else { return $default; }
    } return $cursor;
}

It's a function that will lookup a path in a tree.

echo option_select([A=>[B=>[C=>'hello world']]], 'NO?', 'A', 'B', 'C');

As you can see. I'm looping over the values over a array: path. In the beginning I'm setting a cursor as reference to a array I have to search in. Every time I find a new key, I'm setting the reference of the cursor to the current key I'm searching for. If you have xdebug and a good GUI, you can rewrite it to the algorithm you want. It's the isset($cursor[$key]) you have to change. Or inject it as a function: f($cursor, $key, $default) => (bool)

share|improve this answer
    
You have provided an alternate solution but have not reviewed the code. Please add explanation about why this would be a better solution for the OP, rather than what they already have. – SirPython Aug 29 '15 at 1:45
    
Because it isn't strongly typed SirPython. This way i can test it with unit tests, without making a unit test for every 'hardcoded-string'. So has to with testability and the speed i can create working code. – Roger Keulen Sep 13 '15 at 15:56

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.