public static function previous_by_key($array, $key, $get_value = false, $strict = false)
    {
        if (!is_array($array) and !$array instanceof \ArrayAccess) {
            throw new \InvalidArgumentException('First parameter must be an array or ArrayAccess object.');
        }

        // get the keys of the array
        $keys = array_keys($array);

        // and do a lookup of the key passed
        if (($index = array_search($key, $keys, $strict)) === false) {
            // key does not exist
            return false;
        } // check if we have a previous key
        elseif (!isset($keys[$index - 1])) {
            // there is none
            return null;
        }

        // return the value or the key of the array entry the previous key points to
        return $get_value ? $array[$keys[$index - 1]] : $keys[$index - 1];
    }
public static function previous_by_key($array, $key, $get_value = false, $strict = false)
    {
        if (!is_array($array) and !$array instanceof \ArrayAccess) {
            throw new \InvalidArgumentException('First parameter must be an array or ArrayAccess object.');
        }

        // get the keys of the array
        $keys = array_keys($array);

        // and do a lookup of the key passed
        if (($index = array_search($key, $keys, $strict)) === false) {
            // key does not exist
            return false;
        } // check if we have a previous key
        elseif (!isset($keys[$index - 1])) {
            // there is none
            return null;
        }

        // return the value or the key of the array entry the previous key points to
        return $get_value ? $array[$keys[$index - 1]] : $keys[$index - 1];
    }
public static function previous_by_key ( $ array , $ key , $ get_value = false , $ strict = false ) { if ( ! is_array ( $ array ) and ! $ array instanceof \ ArrayAccess ) { throw new \ InvalidArgumentException ( 'First parameter must be an array or ArrayAccess object.' ) ; } // get the keys of the array $ keys = array_keys ( $ array ) ; // and do a lookup of the key passed if ( ( $ index = array_search ( $ key , $ keys , $ strict ) ) === false ) { // key does not exist return false ; } // check if we have a previous key elseif ( ! isset ( $ keys [ $ index - 1 ] ) ) { // there is none return null ; } // return the value or the key of the array entry the previous key points to return $ get_value ? $ array [ $ keys [ $ index - 1 ] ] : $ keys [ $ index - 1 ] ; }
Get the previous value or key from an array using the current array key

@param   array $array the array containing the values
@param string $key if true, do a strict key comparison

@param bool $get_value
@param bool $strict
@throws \InvalidArgumentException
@return  mixed  the value in the array, null if there is no previous value, or false if the key doesn't exist
Get the previous value or key from an array using the current array key