/** * Fetch the value associated with the given key if it exists. * * @param array|Traversable|AssociativeInterface $collection * @param mixed $key The key to fetch. * @param mixed &$value Assigned the value associated with $key if it exists. * * @return boolean True if $key exists and $value was populated; otherwise, false. */ public static function tryGet($collection, $key, &$value) { if ($collection instanceof AssociativeInterface) { return $collection->tryGet($key, $value); } elseif (is_array($collection)) { if (array_key_exists($key, $collection)) { $value = $collection[$key]; return true; } return false; } return static::any($collection, function ($k, $v) use($key, &$value) { if ($k === $key) { $value = $v; return true; } return false; }); }