I'm pretty new to regex so I want to ask a problem related to it. Basically I want to change only value part of an JSON string with certain key. Currently i am using For example : { "id":1, "newId":2,..."age":20}
. I need to replace both of "id" and "newId"'s value to something like {"id":encrypt(1),"newId":encrypt(2)...,"age":20}
. How can I achieve that in PHP using regex ? Thank in advance.
You can change a value inside a json
object as follows:
$json = json_decode('{ "id":1, "newId":2, "age":20}', true);
$json['id'] = 11;
$json['newId'] = 22;
print_r($json);
Output:
Array
(
[id] => 11
[newId] => 22
[age] => 20
)
Notes: