1

I have a project that I need to echo $variable['key'];

Some times $variable['key'] is not exist. And Those variables creates errors when I echo $variable['key']; directly.

My current method is

echo (isset($variable['key'])) ? $variable['key'] : '';

And this is not very efficient way of doing that. I do not want to write all keys two times.

I need a function. Basically That checks the $varible['key']; inside the function and returns the value of it.

function get_ifisset(&$var = null){
if(isset($var)){ return $var; }
}

echo get_ifisset($vars['key']);

but because of $variable['key'] is not exist I can not send them inside to the function

This kind of usage throws error which is "undefined key".

Also following function is an another approach that I dont like.

function get_ifisset($var, $key){
if(array_key_exists($key, $GLOBALS[$var])){ return $GLOBALS[$var][$key]; }
}

I would like to learn Are there any way to check exitency of an array key inside the function.

property_exists(); array_key_exists(); isset();

Deniz Porsuk
  • 492
  • 1
  • 6
  • 20
  • Whats the problem is with the "another approach"? Why don't you like that? – vaso123 Dec 16 '14 at 11:22
  • 1
    there is two ways to do it: use `isset` like a good boy or disable `E_NOTICE` warnings (this is bad) – Peter Dec 16 '14 at 11:22
  • @Peter do you think that this is efficient? – Deniz Porsuk Dec 16 '14 at 11:24
  • I'll be honest. I supress E_NOTICE warnings. I know this is BAD practice but I don't care. My life is much much easier. If you are not lazy **** as me use `isset()` everywhere. – Peter Dec 16 '14 at 11:24
  • @lolka_bolka I do not like because I can not check $var['notexist']['notexistanother']; with that. This also throws error : undefined index. – Deniz Porsuk Dec 16 '14 at 11:26
  • @DenizPorsuk - that's why you use `isset($var['key']) && isset($var['key']['another'])`. However, if you want to chain such an expression, Peter provided a proper answer. – N.B. Dec 16 '14 at 11:48

2 Answers2

1

You can pass array, key and default value to function:

public function getValue($array, $key, $default = '') {
    return isset($array[$key]) ? $array[$key] : $default;
}

$this->getValue($variable, 'key');

Also, from my experience function get_ifisset(&$var) does not throw any error (I have not set it default to null).

Justinas
  • 41,402
  • 5
  • 66
  • 96
  • Thank you for answer but these kind of approach also can not checks for $var['notexist']['notexistanother']; if I try to send $this->getValue($var['notexist'], 'notexistanother') – Deniz Porsuk Dec 16 '14 at 11:30
  • 1
    @DenizPorsuk you can modify if you pass `$key` as array it will check multi-dimensional key. for example `getValue($array, Array("key", "subkey", "subsubkey"), "");` – Peter Dec 16 '14 at 11:31
  • @Peter this is really not a bad idea. – Deniz Porsuk Dec 16 '14 at 11:36
1

modified @Justinas function:

public function getValue($array, $key, $default = '') {
    if(!is_array($key)) {
        return isset($array[$key]) ? $array[$key] : $default;
    }
    $arr = & $array;
    foreach($key as $subkey) {
        if(!isset($arr[$subkey])) {
            return $default;
        }
        $arr = & $arr[$subkey];
    }
    return $arr;
}

so you can use it on multidimensional arrays

getValue($array, "key");

getValue($array, Array("key", "subkey", "subsubkey"));
Peter
  • 16,453
  • 8
  • 51
  • 77
  • It works for print_r(getValue($test['key'], Array("key2"))); http://ideone.com/juDXyg but not for print_r(getValue($test['notexist'], Array("key2"))); http://ideone.com/O03LRD – Deniz Porsuk Dec 16 '14 at 11:53
  • @DenizPorsuk this is how you should use it... http://ideone.com/1MeeBf because that was the point... – Peter Dec 16 '14 at 12:07