How do I pass array values to a function?

Discussion in 'PHP' started by dwest, Dec 20, 2006.

  1. #1
    Hi,
    I'm new to PHP and not really much of a programmer. But I'm a good explainer :)

    I have a function like so:
    add_option($name, $value, $description, $autoload);

    I want to pass an array to it as follows:

    'rup_prelink_msg', '', 'First segment of notification email', 'yes'

    'rup_link', '', 'Link to pdf retreival page', 'yes'

    'rup_postlink_msg', '', 'Last segment of notification email', 'yes'

    'rup_subject', '', 'Subject of notification email', 'yes'

    'rup_support_email', '', 'From address for notification email', 'yes'

    How would that actually be done in with code??

    Thanks!
     
    dwest, Dec 20, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    You maybe can't just do it like that. You would probably have to modify your function for that. Maybe something like this if I understand you right.

    
    
    add_option($options)
    {
         list($name, $value, $description, $autoload) = $options;
    
         // ... rest of function
    }
    
    
    // Usage:
    
    $details = array('rup_prelink_msg', '', 'First segment of notification email', 'yes');
    
    add_option($details);
    
    
    
    PHP:
     
    nico_swd, Dec 21, 2006 IP
  3. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    OK.
    Here is what's driving this. The function is built into wordpress and is used to store the options into the database, one row for each option.

    Typically one just adds the code like so:

    add_option('rup_prelink_msg', '', 'First segment of notification email', 'yes');
    add_option('rup_link', '', 'Link to pdf retreival page', 'yes');
    add_option('rup_postlink_msg', '', 'Last segment of notification email', 'yes');
    add_option('rup_subject', '', 'Subject of notification email', 'yes');
    add_option('rup_support_email', '', 'From address for notification email',);

    Resulting in five rows added to the database.

    But the following suggestion is made in the WordPress Codex:

    "If you are storing more than one option for your plugin, it would be more efficient to pass an array of option settings to add_option(), which will be stored as a single row of database data."

    So I'm trying to learn the more efficient method.

    Any ideas?

    Thanks!
     
    dwest, Dec 21, 2006 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    like so

    
    <?
    function add_option($options)
    {
    echo "<pre>";
    print_r($options);
    }
    
    $data = array
    (
    "one" => array("rup_prelink_msg", "", "First segment of notification email", "yes"),
    "two" => array("rup_link", "", "Link to pdf retreival page", "yes"),
    "three" => array("rup_postlink_msg", "", "Last segment of notification email", "yes"),
    "four" => array("rup_subject", "", "Subject of notification email", "yes"),
    "five" => array("rup_support_email", "", "From address for notification email", "yes")
    );
    
    add_option($data);
    
    PHP:
     
    krakjoe, Dec 21, 2006 IP
  5. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks Joe!
    Somehow I knew it would be a few simple lines of code.
    Now if I can just figure out what it's doing :)
     
    dwest, Dec 21, 2006 IP
  6. pixel_perfect

    pixel_perfect Banned

    Messages:
    238
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    that's double array the joe wrote or you can pass by single array!
     
    pixel_perfect, Dec 21, 2006 IP
  7. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Two questions:

    Single array...how so?

    and

    I actually got the array code to work but the database simply had a new record with the option_name field value of "array".

    I really don't see how a single record can have multiple values as described in the original motive for attempting to do this.

    Can anyone shed light?

    Thanks!
     
    dwest, Dec 21, 2006 IP