2 回答
TA贡献1865条经验 获得超7个赞
事情不是这样的array_replace_recursive
;替换数组需要与您要替换的数组具有相同的结构。对于您的示例数据,这将起作用:
$json = '{"language":"en","education":[{"unit":"e1","language":"en"}],"membership":[{"uei":"21","Uifed":"failed"}]}';
$arrayData = json_decode($json, true);
$replacementData = array('membership' => array(array('Uifed' => 'success')));
$newArrayData = array_replace_recursive($arrayData, $replacementData);
print_r($newArrayData);
输出:
Array
(
[language] => en
[education] => Array
(
[0] => Array
(
[unit] => e1
[language] => en
)
)
[membership] => Array
(
[0] => Array
(
[uei] => 21
[Uifed] => success
)
)
)
array_walk_recursive
可以用来做你想做的事:
$replacementData = array('Uifed' => 'success');
array_walk_recursive($arrayData, function (&$value, $key) use ($replacementData) {
$value = $replacementData[$key] ?? $value;
});
print_r($arrayData);
输出:
Array
(
[language] => en
[education] => Array
(
[0] => Array
(
[unit] => e1
[language] => en
)
)
[membership] => Array
(
[0] => Array
(
[uei] => 21
[Uifed] => success
)
)
)
TA贡献1830条经验 获得超9个赞
通过引用foreach传递值的简单示例:
$json = '{"language":"en","education":[{"unit":"e1","language":"en"}],"membership":[{"uei":"21","Uifed":"failed"}]}';
$arrayData = json_decode($json, true);
foreach ($arrayData['membership'] as &$membership) {
if ('failed' === $membership['Uifed']) {
$membership['Uifed'] = 'success';
}
}
print_r($arrayData);
小提琴。
- 2 回答
- 0 关注
- 106 浏览
添加回答
举报