who can do this without the recursion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux