VBScript array_slice function

A VBScript equivalent of PHP’s array_slice


Function array_slice(mAry,offset,level)

    array_slice = false

    If Not isArray(mAry) Then Exit Function
    If Not isNumeric(offset) Then Exit Function
    If Not isNumeric(level) Then level = uBound(mAry)

    Dim s,e,arynum
    arynum = uBound(mAry)

    If offset >= 0 Then _
        s = offset _
    Else _
        s = arynum + offset + 1

    If level >= 0 Then _
        e = s + level _
    Else _
        e = arynum + level

    If e > arynum Then e = arynum

    Dim i,counter
    counter = 0
    ReDim tmp_ar(e-s)
    For i = s to e
        tmp_ar(counter) = mAry(i)
        counter = counter +1
    Next

    array_slice = tmp_ar

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