VBScript array_walk_recursive function

A VBScript equivalent of PHP’s array_walk_recursive


Function array_walk_recursive(ByRef arr, callback, userdata)

    Dim key
    Dim thisCall

    If Len( callback ) = 0 Then Exit Function

    If isArray( arr ) Then

        For key = 0 to uBound( arr )
            If isArray(arr(key)) or isObject(arr(key)) Then
                thisCall = "array_walk_recursive"
            Else
                thisCall = callback
            End If

            execute("call " & callback & "(arr(key),key,userdata)")
        Next

    ElseIf isObject( arr ) Then

        For Each key In arr
            If isArray(arr(key)) or isObject(arr(key)) Then
                thisCall = "array_walk_recursive"
            Else
                thisCall = callback
            End If

            execute("call " & callback & "(arr(key),key,userdata)")
        Next

    End If

    array_walk_recursive = true

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