合并PHP中的两个复杂对象 [英] Merging two complex objects in PHP

查看:65
本文介绍了合并PHP中的两个复杂对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个从JSON转换的数据对象.两者都非常复杂,我想以类似于jQuery使用扩展合并两个对象的方式来合并它们.

I have two data objects converted from JSON. both are quite deeply complex and I'd like to merge them in a similar fashion to how jQuery would merge two objects using extend.

示例

JSON 1:

{
    ...
    "blah":
    {
        "params":
        {
            "foo":
            {
                "default": "bar",
                "misc": "0",
                ...
            },
            ...
        },
        ...
    },
    ...
}

JSON 2:

{
    ...
    "blah":
    {
        "params":
        {
            "foo":
            {
                "value": "val",
                "misc": "1",
                ...
            },
            ...
        },
        ...
    },
    ...
}

合并到

{
    ...
    "blah":
    {
        "params":
        {
            "foo":
            {
                "default": "bar",
                "value": "val",
                "misc": "1",
                ...
            },
            ...
        },
        ...
    },
    ...
}

使用PHP对象解决此问题的最佳方法是什么.

What's the best way to approach this with PHP objects.

推荐答案

将每个JSON字符串解码为一个关联数组,合并结果并重新编码

Decode each JSON string into an associative array, merge the results and re-encode

$a1 = json_decode( $json1, true );
$a2 = json_decode( $json2, true );

$res = array_merge_recursive( $a1, $a2 );

$resJson = json_encode( $res );

更新: 如果您有特定的合并要求,则需要编写自己的合并功能. 我在下面写了一篇满足您问题中所述要求的书. 如果您有其他未提及的要求,则可能需要对其进行调整.

Update: If you have a specific merge requirement then you'll need to write your own merging function. I have written one below which fulfills your requirements as stated in the question. You may need to tweak it if you have other requirements you haven't mentioned.

<?php

$json1 = '
{
    "blah":
    {
        "params":
        {
            "foo":
            {
                "default": "bar",
                "misc": "0"
            }
        },
        "lost":
        {
            "one": "hat",
            "two": "cat"
        }
    }
}';

$json2 = '
{
    "blah":
    {
        "lost": "gone",
        "params":
        {
            "foo":
            {
                "value": "val",
                "misc": "1"
            }
        }
    },
    "num_array": [12, 52, 38]
}';


$a1 = json_decode( $json1, true );
$a2 = json_decode( $json2, true );


/*
 * Recursive function that merges two associative arrays
 * - Unlike array_merge_recursive, a differing value for a key
 *   overwrites that key rather than creating an array with both values
 * - A scalar value will overwrite an array value
 */
function my_merge( $arr1, $arr2 )
{
    $keys = array_keys( $arr2 );
    foreach( $keys as $key ) {
        if( isset( $arr1[$key] ) 
            && is_array( $arr1[$key] ) 
            && is_array( $arr2[$key] ) 
        ) {
            $arr1[$key] = my_merge( $arr1[$key], $arr2[$key] );
        } else {
            $arr1[$key] = $arr2[$key];
        }
    }
    return $arr1;
}


$a3 = my_merge( $a1, $a2);
$json3 = json_encode( $a3 );
echo( $json3 );

/*
{
    "blah":
    {
        "params":
        {
            "foo":
            {
                "default": "bar",
                "misc":    "1",
                "value":   "val"
            }
        },
        "lost": "gone"
    },
    "num_array": [12,52,38]
}
*/

这篇关于合并PHP中的两个复杂对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆