|
Posted by jerrygarciuh on 06/10/07 02:36
Well I could do that except that it would mean re-writing dozens more
script than I currently have to. I was more looking to see if anyone
had a more elegant solution than the one I posted.
Thanks for the reply!
JG
On Jun 8, 3:39 pm, ZeldorBlat <zeldorb...@gmail.com> wrote:
> On Jun 8, 1:33 pm, jerrygarciuh <jerrygarc...@gmail.com> wrote:
>
>
>
> > Hello,
>
> > I am converting a large scale app from Perl TemplateToolkit and
> > Class::DBI to PHP.
>
> > TemplateToolkit has elegant ways of coping with variables that may be
> > a scalar or may be an array. So my predecessor has a number of vars
> > that are ambiguous in this way.
>
> > My question to you is whether, given this ambiguity, there is a better
> > way to do this in PHP than my current solution?
>
> > // $a is sometimes an array
> > $a = array('foo', 'bar', 'kermit ruffins');
>
> > if (is_array($a)) {
> > foreach($a as $b) {
> > if (preg_match('/ruffin/', $b)) {
> > echo 'ruffin found';
> > }
> > }
> > } else {
> > if (preg_match('/ruffin/', $a)) {
> > echo 'ruffin found';
> > }
> > }
>
> > Much obliged for any help!
>
> > Peace,
>
> > JG
>
> Just make it an array if it isn't then handle it as an array
> everytime:
>
> // $a is sometimes an array
> $a = array('foo', 'bar', 'kermit ruffins');
>
> if(!is_array($a))
> $a = array($a);
>
> foreach($a as $b) {
> if (preg_match('/ruffin/', $b)) {
> echo 'ruffin found';
> }
>
> }
[Back to original message]
|