VBScript array_keys function

A VBScript equivalent of PHP’s array_keys


Function array_keys(mAry,search_value,strict)

    Dim tmp_arr
    Dim key
    Dim include
    Dim addArr
    Dim cnt : cnt = 0

    addArr = true
    If [==](search_value,empty) Then
        addArr = false
        ReDim tmp_arr( count(mAry,0)-1 )
    End If

    If isObject( mAry ) Then

        For Each key In mAry
            include = true
            If [!=](search_value,empty) Then
                If strict = true Then
                    If [!=](mAry(key) , search_value) or (varType(mAry(key)) <> varType(search_value)) Then
                        include = false
                    End If
                ElseIf [!=](mAry(key) , search_value) Then
                    include = false
                End If
            End If

            If include = true Then
                If addArr Then
                    [] tmp_arr, key
                Else
                    tmp_arr(cnt) = key
                    cnt = cnt + 1
                End If
            End If
        Next

    ElseIf isArray(mAry) Then

        For cnt = 0 to uBound(mAry)

            include = true
            If [!=](search_value,empty) Then

                If strict = true Then
                    If [!=](mAry(cnt) , search_value) or (varType(mAry(cnt)) <> varType(search_value)) Then
                        include = false
                    End If
                ElseIf [!=](mAry(cnt) , search_value) Then
                    include = false
                End If
            End If

            If include = true Then
                If addArr Then
                    [] tmp_arr, cnt
                Else
                    tmp_arr(cnt) = cnt
                End If
            End if
        Next
    End If

    array_keys = tmp_arr

End Function

Please also note that php.vbs offers community built functions and goes by the McDonald’s Theory. We’ll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just:

Other PHP functions in the array extension