2 回答
TA贡献1942条经验 获得超3个赞
您可以使用键和NULL(或任何您需要的)作为值来定义初始数组:
$entities = [
'deliveredAt' => null,
'issuedAt' => null,
'totals' => [
'due' => null,
'gross' => null,
'net' => null,
'tax' => [
'amount' => null,
'net' => null,
'rate' => null,
],
]
];
// here's your real data
$realData = [
"issuedAt" => "2020-08-20",
"totals" => [
"tax" => [
'net' => 42,
]
]
];
// now use array_replace_recursive to replace keys in `$entities` with values of `$realData`
print_r(array_replace_recursive($entities, $realData));
小提琴。
另请注意,$realData不存在的键$entities将被添加到结果中。
TA贡献1856条经验 获得超11个赞
您可以使用array_replace_recursive来执行此操作。您只需稍微更改关联数组实体,因此每个属性都需要初始化(例如 NULL 或 '')。
$result = array_replace_recursive($entities, $array);
在这里您可以测试它http://sandbox.onlinephpfunctions.com/code/4688ed3240050479edeef7c9e4da16f98dbe01de
这是孔代码:
$array = [
"issuedAt" => "2020-08-20",
"totals" => [
"tax" => [
'amount' => 100
]
]
];
$entities = [
'deliveredAt' => NULL,
'issuedAt' => NULL,
'totals' => [
'due' => NULL,
'gross' => NULL,
'net' => NULL,
'tax' => [
'amount' => NULL,
'net' => NULL,
'rate' => NULL
],
]
];
$result = array_replace_recursive($entities, $array);
var_dump($result);
- 2 回答
- 0 关注
- 107 浏览
添加回答
举报