Results 1 to 5 of 5

Thread: Passing an array into arguments

  1. #1
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default Passing an array into arguments

    Yes I googled this, but I could not find anything. Here is my function that is in a class:
    PHP Code:
            public function create()
            {
                
    $args func_get_args();
                
    $element $args[0];
                if(isset(
    $this->elements->$element)) {
                    
    $value=$this->elements->$element;
                    unset(
    $args[0]);
                    
    $format implode(", "$args);
                    
    printf($value$format);
                    return 
    true;
                }
                else {
                    return 
    false;
                }
            } 
    My problem is that I want to replace each argument from the original create() function into printf. $this->elements contains an object with formatted elements, ready for printf. I have gotten this to work with one argument, but some elements require multiple ones. How do I pass an array into arguments that a function will read each as it's own argument?
    Last edited by fileserverdirect; 09-21-2010 at 10:19 PM.
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I'm not sure I understand the problem.

    Can you do a foreach loop with a second function?

    func1() {
    $a = getargs;
    foreach($a as $item) {
    func2($item);
    }
    }
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    I should have been more clear, I want an values in an array to be attributes in a function. Like:
    PHP Code:
    $eg = array("attribute2""attribute3"
    then have each become a actually attribute into a function once:
    PHP Code:
    printf($always-here"attribute2""attribute3"
    The last two can change dynamicly, depending on how many values are in the array.
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  4. #4
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    Ok, I figured it out. For those who want to know:
    PHP Code:
    call_user_func_array($function_name$attributes_in_an_array); 
    *Works with built in functions*
    http://php.net/manual/en/function.ca...func-array.php

    Edit:
    Apparently there is ANOTHER function called vprintf, which takes an array directly!
    Last edited by fileserverdirect; 09-21-2010 at 11:48 PM.
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ah, I see. That can be tough, but it looks like that solution works fine.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •