Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3950550
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:37:29+00:00 2026-05-20T01:37:29+00:00

Named parameters are great for a long list of options in a PHP user

  • 0

Named parameters are great for a long list of options in a PHP user defined function or class. But what about nested options?

Eg:

function foobar($foo,$bar,$options=array()) {

    $default_options = array('option1'=>'','option2'=>'hello','option3'=>array('option1'=>true,'option2'=>''));

    $options = array_merge($default_options,(array)$options);
}

So option 3 is another array with suboptions. Would we need to put them in a for loop to merge the suboptions too? What would you guys do in this case?

EDIT:

This is the function call:

foobar('foo','bar',array('option1'=>'foo','option3'=>array('option1'=>false)));

Ending structure for $options:

array(
'option1'=>'foo',
'option2'=>'hello',
'option3'=>array(
    'option1'=>false,
    'option2'=>''
);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:37:29+00:00Added an answer on May 20, 2026 at 1:37 am

    Why not just use the func_get_args function?

    function yourFunction($keys) {
        $default_options = array('test', array('test1' => array('test2')), 'test3');
        $options = func_get_args();
        array_shift($options);
    
        $options = array_combine($keys, $options); 
        $options = array_merge($default_options, $options);
    
        print_r($options);
    }
    
    // Usage
    yourFunction(array('option1', 'option2', 'option3', 'option4'), 'optionval1', array('optionval2-1', 'optionval2-2'), 'optionval3', 4);
    

    This way you have an array to start with and do not need to worry about anything else, and it can accept any number of parameters / arguments!

    EDIT:

    (Second edit, added the $keys as the first param)
    Modified the function above, here is the output. Not sure exactly what format of output you are looking for. So you may need to post the ending array structure you are after for your initial data given.

    Array
    (
        [0] => test
        [1] => Array
            (
                [test1] => Array
                    (
                        [0] => test2
                    )
    
            )
    
        [2] => test3
        [option1] => optionval1
        [option2] => Array
            (
                [0] => optionval2-1
                [1] => optionval2-2
            )
    
        [option3] => optionval3
        [option4] => 4
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using named parameters in a query to match fields in a map-like data
I have user control named DateTimeUC which has two textboxes on its markup: <asp:TextBox
I defined a record named log . I want to create an mnesia table
first of all let me say that I know about dynamic, no PIA, named
I've just started using dotnetopenauth for my user registration. It's great, and is working
I've looked at both the Named Parameter Idiom and the Boost::Parameter library . What
I want in a good performance way (I hope) replace a named parameter in
When you assign a date to a named SQL parameter Hibernate automatically converts it
What's the best method to pass parameters to SQLCommand? You can do: cmd.Parameters.Add(@Name, SqlDbType.VarChar,
A Named Pipe Server is created with hPipe = CreateNamedPipe( zPipePath, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE |

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.