|
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]
|