2

I have a function that takes an "Options" argument array for setting flags for querying a database. It looks like this

function c_SQL($SQL='', $FIELDS=array(), $OPTIONS=array('SINGLEROW'=>false,'foo'=>false,'bar'=>false)) {

}

I am trying to maintain the default array keys if I do not set them in my function call:

$test = c_SQL($query,$fields,array('SINGLEROW'=>true));

This is generating an error inside the c_SQL function when we do checks against the array keys not specified (foo and bar).

Is there a way to maintain 'foo' and 'bar' if not specified and only change the keys only if passed into the function?

2
  • Have you tried using the isset function? If the result is false, you could always manually set the variable there. Commented May 14, 2015 at 19:24
  • That was what I was leaning towards doing if there was not a way to maintain the defaults. We're just dealing with a lot of "potential" options being passed in and were trying to avoid checking each one Commented May 14, 2015 at 19:25

4 Answers 4

5

You can use array_merge inside the function to merge with a defaults array defined inside the function.

function c_SQL($SQL = '', $FIELDS=array(), $OPTIONS = array()) {
    $defaults =  array(
        'SINGLEROW' => false,
        'foo' => false,
        'bar' => false  
    );
    $OPTIONS = array_merge($defaults, $OPTIONS);
    return $OPTIONS;
}

#  array ( 'SINGLEROW' => false, 'foo' => false, 'bar' => false, )
var_export(c_SQL());

# array ( 'SINGLEROW' => true, 'foo' => false, 'bar' => false, )
var_export(c_SQL(array('SINGLEROW' => true)));
Sign up to request clarification or add additional context in comments.

Comments

3

You could hold the defaults as a constant, and use array_merge to apply them to the user's input:

$DEFAULT_OPTIONS = array('SINGLEROW'=>false,'foo'=>false,'bar'=>false);
function c_SQL($sql='', $fields=array(), $options=array()) {
    $actual_options = array_merge($default_options, $options);
}

5 Comments

Excellent suggestion! I like this idea, as it will only overwrite the ones they specified.
@BryanZwicker This looks good but I think $default_options should go before $options in the call to array_merge because the second array overwrites the first array and you want your new options to override the defaults.
@Andrew I always mix the too up ;-( You are correct though - I fixed the answer accordingly. Thanks!
I noticed when I looked at the docs for array_merge(). I figured it was just a pseudo-code oversight :) Thanks again!
$default_options should be inside the scope of the function. It's useless anywhere else and there's a risk of a future conflict with another variable. See my solution.
1

Create your default options inside the function body and then merge your new options with array_merge

function c_SQL($SQL='', $FIELDS=array(), $OPTIONS=array()) {
    $DEFAULTS = array('SINGLEROW'=>false,'foo'=>false,'bar'=>false);
    $OPTIONS = array_merge($DEFAULTS, $OPTIONS);
}

1 Comment

Thank you for the suggestion! I like this method, it accomplishes exactly what I was trying to do. @Mureinik beat you to the punch by a few seconds, but +1
1

Try assigning $OPTIONS as null or empty array and then check in function check if it is null or not, if its null then assign the array. Take a look at it here..

function c_SQL($SQL='', $FIELDS=array(), $OPTIONS=NULL) {
    if(empty($OPTIONS) {
        $OPTIONS=array('SINGLEROW'=>false,'foo'=>false,'bar'=>false)
    } else {
       $othervalues = array('SINGLEROW'=>false,'foo'=>false,'bar'=>false);
       $OPTIONS = array_merge($othervalues, $OPTIONS);
       //do something here
    }
}

1 Comment

If the passed array had only one or two of the three required options, it would still pass your condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.