Helper function that returns the value for a key in an array or a property in an object. No more endless isset() statements.
<?php
/**
* Return the value for a key in an array or a property in an object.
* @param mixed $haystack
* @param string $needle
* @param mixed $default_value The value if key could not be found.
* @return mixed
*/
function get_key ($haystack, $needle, $default_value = '')
{
if (is_array($haystack)) {
// We have an array. Find the key.
return isset($haystack[$needle]) ? $haystack[$needle] : $default_value;
}
else {
// If it's not an array oit must be an object
return isset($haystack->$needle) ? $haystack->$needle : $default;
}
}