VBScript array_fill_keys function

A VBScript equivalent of PHP’s array_fill_keys


Function array_fill_keys(keys, val)

    Dim ary_fill,i
    set ary_fill = Server.CreateObject("Scripting.Dictionary")
    set array_fill_keys = ary_fill
    if Not isArray(keys) then Exit Function
    If isArray(val) Then
        If uBound(val) > uBound(keys) then Exit Function
    End If

    If IsArray(val) Then
        For i = 0 to uBound(keys)
            If Not ary_fill.Exists( keys(i) ) Then ary_fill.Add keys(i), val(i)
        Next
    Else
        For i = 0 to uBound(keys)
            If Not ary_fill.Exists( keys(i) ) Then ary_fill.Add keys(i), val
        Next
    End If

    set array_fill_keys = ary_fill

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