2

I have an array holding some default settings for my plugin. As the plugin evolves settings maybe removed or added from version to version.

Here is an example default array:

$defaults = array(
                    'setting1' => 'somevalue',
                    'setting2' => 'somevalue',
                    'setting4' => 'somevalue',
                  );

Here is an example of live settings data that the structure needs to be updated for the new $default structure:

$livesettings = array(
                        'setting1' => 'foo',
                        'setting2' => 'bar',
                        'setting3' => 'foobar',
                      );

I'm looking for a function where I can pass both arrays and the structure of the livesettings is updated to match the $defaults.

So in this case in livesettings:

  1. setting1 and setting2 isn't touched. Their values stay intact
  2. setting3 is removed as no longer needed
  3. setting4 is added with the default value of somevalue

Are their any functions in PHP that can do this in one go? If yes what is it? If no how would I achieve this with PHP code?

3
  • you basically want to merge the 2 arrays. try adding a cache mechanism, as i dont think they change that often Commented May 20, 2011 at 11:13
  • Your question is confusing, I was expecting $livesettings to be the actual dataset you are building because you add setting 4, but then why is setting 3 removed? Commented May 20, 2011 at 11:25
  • Image a plugin in use. The admin of the plugin sets the settings for this plugin stored in $livesettings. I publish a new plugin version that no longer uses/needs setting3 but now has setting4. I need to update the users settings to match this new structure without resetting the uses original settings. Commented May 20, 2011 at 11:32

2 Answers 2

8

You want a combination of array_intersect_key() and array_merge().

$livesettings = array_intersect_key($livesettings, $defaults);
$livesettings = array_merge($defaults, $livesettings);

The first function will remove all keys not found in $defaults, while the second would add items from $defaults not found in $livesettings

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

Comments

1

you don't need to a function for this problam, you can also use the $defaults like a base array,

    $defaults = array(
                        'setting1' => 'somevalue',
                        'setting2' => 'somevalue',
                        'setting4' => 'somevalue',
                      );

    $livesettings = $defaults; // it will be copited by value,

    $livesettings['setting1']  = 'overriden setting 1';
    $livesettings['setting3'] = 'added new setting to live config';

3 Comments

I dont want $livesettings values overridden by $defaults values. I just want the key structure mapped from defaults to $livesettings
overriden is only for $livesettings array, $default will not be changed.
Sorry you are completely missing the point. Understood as my question I feel could be a bit more clearer.

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.