2 回答

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊
來自關(guān)于 map 方法的文檔:
返回對(duì)每個(gè)值應(yīng)用回調(diào)的結(jié)果
和:
可調(diào)用對(duì)象應(yīng)返回鍵將在結(jié)果映射中映射到的內(nèi)容。
這意味著您只需要返回修改value后的數(shù)組而不是數(shù)組{key:value}。
這是我機(jī)器上的一個(gè)工作示例(PHP 7.0 CLI):
$json_template = json_decode('
{
"username": "",
"password": "",
"environment": "900"
}
', true);
$map = new \Ds\Map($json_template);
$maps = $map->map(function($key, $value){
if($key == 'password'){
$value = 'test';
}
return $value;
});
print_r($maps->toArray());
輸出:
Array
(
[username] =>
[password] => test
[environment] => 900
)

TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊
你可以這樣做:
$json_template = json_decode($json_body,true);
foreach($json_template as $key => $value){
if($key == 'password'){
$json_template[$key]= 'test';
}
}
希望能幫助到你。
- 2 回答
- 0 關(guān)注
- 193 瀏覽
添加回答
舉報(bào)