S
S
Steve2021-07-01 13:51:47
PHP
Steve, 2021-07-01 13:51:47

How to replace individual values ​​in a json string?

i have a json string

{"ops":[{"url":"site.com/100/test"},{"url":"site.com/200/test"}]}


how to copy this string in the same way but replace the values ​​100 and 200 with 1000 and 2000 and return the string

{"ops":[{"url":"site.com/1000/test"},{"url":"site.com/2000/test"}]}

?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fedor Vlasenko, 2021-07-01
@CenterJoin

$json = '{"ops":[{"url":"site.com/100/test"},{"url":"site.com/200/test"}]}';
$json = str_replace([100, 200], [1000, 2000], $json);
echo $json;
//{"ops":[{"url":"site.com/1000/test"},{"url":"site.com/2000/test"}]}

str_replace

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question