php - 在 PHP 中合并两个复杂对象

标签 php json

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

例子

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 对象解决此问题的最佳方法是什么。

最佳答案

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

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

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

$resJson = json_encode( $res );

更新: 如果您有特定的合并要求,那么您需要编写自己的合并函数。 我在下面写了一个可以满足您在问题中所述的要求的文章。 如果您有其他未提及的要求,您可能需要对其进行调整。

<?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 - 在 PHP 中合并两个复杂对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9241800/

相关文章:

php - 从数据库 PHP 填充下拉列表

php - Laravel : htaccess is ignored on homestead?

php - 如何从config.php获取数据库名称

javascript - 将 JSON 对象从 Angular 服务传递给指令

json - 如何在 powershell 中替换 json 文件中的属性

php - 将变量传递给 mysql 查询结果为 "function query on null"

php - 在 Yii 中获取当前 Controller ID

c# - 将字符串添加到逐字字符串文字

java - Dropwizard 读取请求 JSON

c# - 是否有使用 basicHttpBinding 扩展 WCF 服务以允许 REST 服务与 JSON 通信的好方法?