def search_for_matches_in_a_sorted_array(input_array, match_value,
    start_position=0, search_direction=+1, output_allmatches=False):
    """ Example: xmatch2 = search_for_matches_in_a_sorted_array(array2, array1[i1], i2, -1)
    """
    i = start_position
    xmatches = []
    while i >= 0 and i <= len(input_array) - 1:
        val = input_array[i]
        if val == match_value:
            xmatches.append(i)
            if not output_allmatches:
                break
        elif search_direction > 0:
            if val > match_value:
                break
        elif search_direction < 0:
            if val < match_value:
                break
        i = i + search_direction
    return xmatches
