2 回答

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊
您可以使用鍵和NULL(或任何您需要的)作為值來(lái)定義初始數(shù)組:
$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));
小提琴。
另請(qǐng)注意,$realData不存在的鍵$entities將被添加到結(jié)果中。

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊
您可以使用array_replace_recursive來(lái)執(zhí)行此操作。您只需稍微更改關(guān)聯(lián)數(shù)組實(shí)體,因此每個(gè)屬性都需要初始化(例如 NULL 或 '')。
$result?=?array_replace_recursive($entities,?$array);
在這里您可以測(cè)試它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 關(guān)注
- 167 瀏覽
添加回答
舉報(bào)