0

here's my php code:

$settings = array(
    'width'         => 500,
    'height'        => 300
);
$mygrid->init($settings);

class code:

class mygrid
{
    function __construct($settings)
    {
       $default_settings = array(
        'width'         => 500,
        'height'        => 300
       )
    }

}

i'd like to kinda merge both arrays in order to have a fallback to default values, eg. when creating an instance which sets width only:

$settings = array('width' => 800);
$mygrid->init($settings);

the class should use the default height of 300

thanks

1
  • 2
    The question is unclear in its current state. Please edit the text above and add more details, especially what it is you want to do. What does "depending on which variables are passed" mean? Commented Aug 2, 2013 at 7:45

1 Answer 1

3

Have you look at array_merge() ?

function init($settings) {
    $defaults = aray('width' => 250, 'height' => 300, 'color' => 'black');
    $this->settings = array_merge($defaults, $settings);

}

With your example, the result will be:

$this->settings = array('width' => 300, 'height' => 500, 'color' => 'black');

Every key passed in your $settings parameter will override the equivalent in $defaults.

Sign up to request clarification or add additional context in comments.

Comments

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.