Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我对正则表达式很陌生,所以我想问一个与之相关的问题。基本上我只想用某个键更改 JSON 字符串的值部分。目前我正在使用例如: { "id":1, "newId":2,..."age":20}。我需要将“id”和“newId”的值都替换为{"id":encrypt(1),"newId":encrypt(2)...,"age":20}. 如何使用 regex 在 PHP 中实现这一点?预先感谢。
{ "id":1, "newId":2,..."age":20}
{"id":encrypt(1),"newId":encrypt(2)...,"age":20}
json您可以按如下方式更改对象内部的值:
json
$json = json_decode('{ "id":1, "newId":2, "age":20}', true); $json['id'] = 11; $json['newId'] = 22; print_r($json);
输出:
Array ( [id] => 11 [newId] => 22 [age] => 20 )
笔记: