You are here: Re: Variable Argument Function Calls « PHP Programming Language « IT news, forums, messages
Re: Variable Argument Function Calls

Posted by Adam on 08/16/06 08:16

Thanks Rik, that's exactly what I was after!

Cheers,
Adam.


Rik wrote:
> Adam wrote:
> > Thanks for your help so far... I had to type it from memory, so agree
> > that the "case 0:" was dead wrong!
> >
> > The reason for using the multi arg input is twofold...
> >
> > 1. I am placing the vars into sprintf (to construct a MySQL query)
> > which is itself a variable arg function (hence why I don't think I can
> > use an array here).
>
> Euhm. vsprintf()? Takes an array, loves it actually.
>
>
> > 2. Using an array just means I need a few extra lines of code to move
> > all of the random query bits into an intermediate array to stick into
> > the function. I would prefer to be able to list them in the function
> > call.
> >
> > I am aware that there are other ways to do this, but I am aiming for
> > an ultra elegant solution. Maybe I'm being a bit pedantic about
> > it!?! : )
>
>
> Well, the best I can do to achieve what I think you want:
> Call an arbitrary function with an arbitrary amount of arguments, switching
> scalars and arrays like it doesn't care, the first argument given is the
> function, be it in a scalar or the first scalar in the first array (of the
> first array and so forth):
>
> <?php
>
> function flatten_array($arg){
> if(!is_array($arg)) return array($arg);
> $return = array();
> foreach($arg as $row){
> if(is_array($row));
> $return = array_merge($return,flatten_array($row));
> } else {
> $return[] = $row;
> }
> }
> return $return;
> }
>
> function varargsfunction(){
> $array = func_get_args();
> $array = flatten_array($array);
> $function = array_shift($array);
> return call_user_func_array($function,$array);
> }
> ?>
>
> Grtz,
> --
> Rik Wasmus

 

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

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