hi everyone,

I have a function which recursively loops an assoc array in
order to build an HTML string containing hidden <input> elements
that repesent the key/values passed in the array ... I know this can
be done without recursion but I'm having a brainfreeze, anyone care
to share some tips on replacing the recursion with some kind of stack
based solution (the idea being that I want to remove the overhead
of the recursive calls to the function if possible ...

I'm looking to get a better understanding of alternatives to
recursion in general rather than in this specific example,
so come people show us what you can do! :-)

example:

function rec_build_hidden_inputs($args = array(), $prefix = '')
{
    static $inputTpl = "<input type="hidden" name="%s" value="%s" />\n";

    $_contents = '';
    foreach ($args as $key => $val) {
        $nextPrefix = $prefix > ''
                    ? "{$prefix}[{$key}]";
                    : $key
                    ;

        $_contents .= is_array($val)
                    ? rec_build_hidden_inputs($val, $nextPrefix)
                    : sprintf($inputTpl, $nextPrefix, $key)
                    ;
    }

    return $_contents;
}

rgds,
Jochem

PS - do ya think I can copyright this?:

        =
        ?
        :
        ;

nah, didn't think so - none the less you might be surprised how many people
it annoys ;-)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to