lib/PicoTwigExtension.php
public static function getKeyOfVar($var, $keyPath)
    {
        if (!$keyPath) {
            return null;
        } elseif (!is_array($keyPath)) {
            $keyPath = array($keyPath);
        }

        foreach ($keyPath as $key) {
            if (is_object($var)) {
                if ($var instanceof ArrayAccess) {
                    // use ArrayAccess, see below
                } elseif ($var instanceof Traversable) {
                    $var = iterator_to_array($var);
                } elseif (isset($var->{$key})) {
                    $var = $var->{$key};
                    continue;
                } elseif (is_callable(array($var, 'get' . ucfirst($key)))) {
                    try {
                        $var = call_user_func(array($var, 'get' . ucfirst($key)));
                        continue;
                    } catch (BadMethodCallException $e) {
                        return null;
                    }
                } else {
                    return null;
                }
            } elseif (!is_array($var)) {
                return null;
            }

            if (isset($var[$key])) {
                $var = $var[$key];
                continue;
            }

            return null;
        }

        return $var;
    }
public static function getKeyOfVar($var, $keyPath)
    {
        if (!$keyPath) {
            return null;
        } elseif (!is_array($keyPath)) {
            $keyPath = array($keyPath);
        }

        foreach ($keyPath as $key) {
            if (is_object($var)) {
                if ($var instanceof ArrayAccess) {
                    // use ArrayAccess, see below
                } elseif ($var instanceof Traversable) {
                    $var = iterator_to_array($var);
                } elseif (isset($var->{$key})) {
                    $var = $var->{$key};
                    continue;
                } elseif (is_callable(array($var, 'get' . ucfirst($key)))) {
                    try {
                        $var = call_user_func(array($var, 'get' . ucfirst($key)));
                        continue;
                    } catch (BadMethodCallException $e) {
                        return null;
                    }
                } else {
                    return null;
                }
            } elseif (!is_array($var)) {
                return null;
            }

            if (isset($var[$key])) {
                $var = $var[$key];
                continue;
            }

            return null;
        }

        return $var;
    }
public static function getKeyOfVar ( $ var , $ keyPath ) { if ( ! $ keyPath ) { return null ; } elseif ( ! is_array ( $ keyPath ) ) { $ keyPath = array ( $ keyPath ) ; } foreach ( $ keyPath as $ key ) { if ( is_object ( $ var ) ) { if ( $ var instanceof ArrayAccess ) { // use ArrayAccess, see below } elseif ( $ var instanceof Traversable ) { $ var = iterator_to_array ( $ var ) ; } elseif ( isset ( $ var -> { $ key } ) ) { $ var = $ var -> { $ key } ; continue ; } elseif ( is_callable ( array ( $ var , 'get' . ucfirst ( $ key ) ) ) ) { try { $ var = call_user_func ( array ( $ var , 'get' . ucfirst ( $ key ) ) ) ; continue ; } catch ( BadMethodCallException $ e ) { return null ; } } else { return null ; } } elseif ( ! is_array ( $ var ) ) { return null ; } if ( isset ( $ var [ $ key ] ) ) { $ var = $ var [ $ key ] ; continue ; } return null ; } return $ var ; }
Returns the value of a variable item specified by a scalar key or a
arbitrary deep sub-key using a key path

@param array|Traversable|ArrayAccess|object $var     base variable
@param mixed                                $keyPath scalar key or a
array interpreted as key path (when passing e.g. ['foo', 'bar'],
the method will return $var['foo']['bar']) specifying the value

@return mixed the requested value or NULL when the given key or key path
didn't match
Returns the value of a variable item specified by a scalar key or a arbitrary deep sub - key using a key path