Send an array as a variable number of arguments

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Send an array as a variable number of arguments

Post by anjanesh »

Code: Select all

<?php
foo("string-1");
foo(array("string-a", "string-b", "string-c"));

function foo($a, $p1 = "", $p2 = "") # This cannot be changed to variable number of params
 {
        //if (!is_array($a)) $a = array($a);
        foo1($a);
 }

function foo1() # variable number of params
 {
        $args_count = func_num_args();
        $args = func_get_args();

        foreach ($args as $arg)
         {
                echo $arg."\n";
         }
 }
?>
Any way to get this to output ?

Code: Select all

string-1
string-a
string-b
string-c
How do I send an array as a variable number of arguments to a function ?

Thanks
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Code: Select all


implode('', $a);

User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

imlode will combine the array values into a string and send it as a single argument.
foo1(implode('', $a)); will end up as foo1("string-astring-bstring-c");
what I need is it to send it as foo1("string-a", "string-b", "string-c");
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Post by arjan.top »

Try with call_user_func_array()
User avatar
webspider
Forum Commoner
Posts: 52
Joined: Sat Oct 27, 2007 3:29 am

Re: Send an array as a variable number of arguments

Post by webspider »

anjanesh wrote:

Code: Select all

<?php

function foo($a, $p1 = "", $p2 = "") # This cannot be changed to variable number of params
 {
           foo1($a);
 }

function foo1() # variable number of params
 {
       
 }
?>
function foo1($a) is called with argument $a, but in function definition there is no argument. Is this possible in php?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Send an array as a variable number of arguments

Post by Zoxive »

webspider wrote: function foo1($a) is called with argument $a, but in function definition there is no argument. Is this possible in php?

Code: Select all

$args_count = func_num_args();
$args = func_get_args();
Have a look at the manual for those (Rarely used) functions.
User avatar
webspider
Forum Commoner
Posts: 52
Joined: Sat Oct 27, 2007 3:29 am

Post by webspider »

$args is becoming a two dimensional array. but we sent a one dimensional array $a.
then should we output like this :?

Code: Select all

foreach ($args as $arg)
        {
           foreach($arg as $val)
           {
                echo $val."\n";
           }
        }
Post Reply