AW: [PHP] How to use the fields of an array as individual parameter for a function

From: Date: Tue, 04 Mar 2003 10:56:36 +0000
Subject: AW: [PHP] How to use the fields of an array as individual parameter for a function
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-138074@lists.php.net to get a copy of this message
Mh...this works great! Thanks a lot! --- Gruß CB -----Ursprüngliche Nachricht----- Von: Tom Rogers [mailto:trogers@kwikin.com] Gesendet: Dienstag, 4. März 2003 11:49 An: Christian Bartels Cc: php-general@lists.php.net Betreff: Re: [PHP] How to use the fields of an array as individual parameter for a function Hi, Tuesday, March 4, 2003, 7:52:26 PM, you wrote: CB> Hello! CB> I'm new here on the list. I hope you can help me! :) CB> I have a function foo1 which has a parameter $params. In the CB> function i want to pass $params to another function foo2. if $params CB> is an array the elements should be passed to foo2 as individual CB> parameters. Is this possible? How do i do that? CB> Example: CB> function foo1($params) { CB> if (is_array($params)) { CB> $object = new foo2(GET THE ELEMENTS OF $params AS INDIVIDUAL CB> PARAMETERS CB> HERE); CB> } else { CB> $object = new foo2($params); CB> } CB> } CB> So when this is called: CB> foo1(array('blue', 'red', 'green')); CB> In foo1 foo2 should be called like this: CB> foo2('blue', 'red', 'green'); CB> Is this possible? CB> Thanks for any help! CB> (sorry about my english ;-)) CB> --- CB> CB CB> -- CB> PHP General Mailing List (http://www.php.net/) CB> To unsubscribe, visit: http://www.php.net/unsub.php You could use eval, something like this: $num = count($params); $vars = '$subject = new foo2('; if($num > 0) { $i = 0; foreach($params as $var){ $vars .= ($i > 0)? ',':''; $vars .= (is_string($var))? "'".$var."'" : $var; $i ++; } } $vars .= ');'; eval($vars); untested but is similar to something I use as a class loader with unknown number of variables to pass. -- regards, Tom

« previous php.general (#138074) next »