dongxiusuo9881 2013-07-24 06:12
浏览 94
已采纳

合并两个对象并覆盖空属性(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.

  • 写回答

2条回答 默认 最新

  • dqphg40600 2013-07-24 06:16
    关注

    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']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类