VBScript array_pad function

A VBScript equivalent of PHP’s array_pad


Function array_pad(ByVal mAry, pad_size, pad_value)

    If Not isArray( mAry ) Then Exit Function
    If Not isNumeric( pad_size ) Then Exit Function

    Dim pad,aryCounter,newLength,i,intCounter

    If pad_size < 0 Then
        newLength = pad_size * -1
    Else
        newLength = pad_size
    End If
    newLength = newLength -1

    aryCounter = uBound(mAry)
    If newLength > aryCounter Then

        ReDim pad(newLength)
        intCounter = 0
        For i = 0 to newLength
            If pad_size < 0 Then
                If newLength - aryCounter > i Then
                    pad(i) = pad_value
                Else
                    pad(i) = mAry(intCounter)
                    intCounter = intCounter + 1
                End If
            Else
                If i > aryCounter Then
                    pad(i) = pad_value
                Else
                    pad(i) = mAry(i)
                End If
            End If
        Next
    Else
        pad = mAry
    End If

    array_pad = pad

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