合并两个对象并覆盖空属性(PHP)

I have two objects and the way our finder function works (I have to call it twice... 1 to get the config key, value i.e. non multilanguage stuff. And the second call to get the multilanguage stuff) makes them look like this:

[config] => Array
    (
        [cfg] => Config_Model Object
            (
                [id] => 2
                [key] => system.default.main_color
                [value] => #FF7C11
                [deleted] => 0
            )

        [help] => Config_Model Object
            (
                [id] => 
                [key] => 
                [value] => 
                [id_config] => 2
                [name] => Hauptfarbe
                [help] => Die Hauptfarbe Ihres CIs. Der Adminbereich erscheint in dieser Farbe.
                [id_lang] => 1
            )

    )

I want to compine these two objects into one. The code, that gets the stuff looks like this:

public static function get($key)
{
  $config['cfg'] = self::find(array('key' => $key), TRUE);
  $config['help'] = self::findInTable(array(
    'id_lang' => Language_Model::getDefaultLanguage(), 
    'id_config' => $config['cfg']->getId()
  ), self::dbTranslationTable, TRUE);
  return $config;
  // return (object) array_merge((array) $config['cfg'], (array) $config['help']);
}

You can tell by the commented return command, that I tried using array_merge(). The problem with that is, that the empty attributes from [help] override the attributes from [cfg], so they are empty again:

[config] => stdClass Object
    (
        [id] => 
        [key] => 
        [value] => 
        [deleted] => 0
        [id_config] => 2
        [name] => Hauptfarbe
        [help] => Die Hauptfarbe Ihres CIs. Der Adminbereich erscheint in dieser Farbe.
        [id_lang] => 1
    )

Whereas it should rather look like this:

[config] => stdClass Object
    (
        [id] => 2
        [key] => system.defalult.main_color
        [value] => #FF7C11
        [deleted] => 0
        [id_config] => 2
        [name] => Hauptfarbe
        [help] => Die Hauptfarbe Ihres CIs. Der Adminbereich erscheint in dieser Farbe.
        [id_lang] => 1
    )

If you need more information, please let me know.

You need to filter out empty values from your second array, and then merge only what's left onto the first array.

The simplest solution would be:

$config['help'] = array_filter((array) $config['help']);
return (object) array_merge((array) $config['cfg'], (array) $config['help']);

This uses the default behaviour of array_filter(), which just checks whether the values evaluate to false. This will remove empty strings, NULL values, or even the number zero.

A safer solution will actually check for empty strings, like this:

$config['help'] = array_filter((array) $config['help'], function($val) {
    return (string) $val != '';
});
return (object) array_merge((array) $config['cfg'], (array) $config['help']);

Silly me... I really should try EVERYTHING before asking a question. Sorry for the unnecessary use of space... The order of how you pass the variables to array_merge() matters! I just turned it around, now it looks like this:

return (object) array_merge((array) $config['help'], (array) $config['cfg']);

And TA-DA! The array looks just like I wanted.