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条)

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常