You are here: who can do this without the recursion « PHP « IT news, forums, messages
who can do this without the recursion

Posted by Jochem Maas on 09/28/29 11:20

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 ;-)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация