function value($array, $key, $default = null) {
if (is_null($key)) return $array;
$keys = $this->force_array($key);
// short-circuit
if (!is_array($array)) {
return $this->resolve($default);
}
// a flag to remember whether something has been found or not
$found = false;
// To retrieve the array item using dot syntax, we'll iterate through
// each segment in the key and look for that value. If it exists, we
// will return it, otherwise we will set the depth of the array and
// look for the next segment.
foreach ($keys as $key) {
foreach (explode('.', $key) as $segment) {
if (!is_array($array) || !array_key_exists($segment, $array)) {
// did we not find something? mark `found` as `false`
$found = false;
break;
}
// we found something, although not sure if this is the last thing,
// mark `found` as `true` and let the outer loop handle it if this
// *is* the last thing in the list
$found = true;
$array = $array[$segment];
}
// if `found` is `true`, the inner loop found something worth returning,
// which means that we're done here
if ($found) {
break;
}
}
if ($found) {
// `found` is `true`, we found something, return that
return $array;
} else {
// `found` isn't `true`, return the default
return $this->resolve($default);
}
}