|  | Posted by Steve on 01/08/08 04:06 
<adam.timberlake@gmail.com> wrote in message news:4c7dc500-439b-4678-b14c-53db37c2f072@1g2000hsl.googlegroups.com...
 >I was reading this article yesterday:
 > http://www.talkphp.com/advanced-php-programming/1886-how-would-i-apply-htmlentities-every-array-item.html
 >
 > I am wondering.. okay.. we can use array_walk but doesn't that just
 > loop through the items anyway ?? So why not just use the foreach loop
 > instead... is it a shorthand trick or is there any deeper reasoning
 > behind it... Thats my question!
 >
 > I do have another question and that is if it is a shorthand version,
 > having a foreach loop isn't much more in terms of the letters you type
 > and so why have the PHP developers put it in there ??
 >
 > It's puzzling me. Thank you in advance.
 
 i've used both methods of to do the same things. it's really
 6-1-0.5-the-other. i'd think array_walk would be most effective when used in
 abstract instances/circumstances...say inside a class that generates html
 but knows nothing about what it is supposed to create. i'll give you a 'for
 instance' in a minute.
 
 for me though, i prefer foreach as most code reads procedurally to the
 programmer and doesn't require him to look around to find a callback
 function. it's simply easier to follow what's going on in the script...not
 to mention debug.
 
 here's a typical scenario. you have to build a select list...selecting a
 default or user-selected initial value for the list...array_walk makes very
 short work of it:
 
 <?
 function buildOptionList($display, $value, &$options)
 {
 $options[1][] = '<option value="' . $value . '" '         .
 ($value == $options[0] ? 'selected' : '') .
 '>' . $display . '</option>';
 }
 $categories['Select Value 1'] = 'some value A';
 $categories['Select Value 2'] = 'some value B';
 $categories['Select Value 3'] = 'some value C';
 $options    = array();
 $optionList = array($_REQUEST['selectInput'], &$options);
 array_walk($categories, 'buildOptionList', $optionList);
 ?>
 <form method="post">
 <select name="selectInput" style="width:150px;">
 <?= implode("\r\n", $options) ?>
 </select>
 <input type="submit" value="do it">
 </form>
 
 as you can see, you have a reusable callback that works for building any
 select...in about four or 5 lines of code and everything is loosely coupled.
 i've taken this approach. i've also used the same methodology to define the
 form fields used for submission and getting and validating everything. i've
 taken those results and generated insert/update/delete statements
 dynamically using the same thing. essentially, what i had at the end was a
 php template. i could certainly better separate the php from the html in
 these scripts, however, they're harder to maintain than their foreach'ed
 counter-parts...even though there is a bit less code.
 
 anyway, just going on at the mouth. functionally, i don't see an advantage
 to either one. from a managability standpoint, i tend to go with foreach.
 your preferences may differ. just realize that the choice of which to use is
 usually just that...a choice.
  Navigation: [Reply to this message] |