gpt4 book ai didi

php - 如何在 PHP 中的 JSON 中添加属性?

转载 作者:行者123 更新时间:2023-12-01 10:00:14 25 4
gpt4 key购买 nike

我在 PHP 中有这样的 Json:

$json = '{"total":"100", "page":"1", "records":"100", "rows": [ 
{"no":"1","part_number":"2","part_name":"3","price":"4","note":"8"}]}';

我想补充
"test1":"5","test2":"7"

到上面的JSON。

所以它会是这样的:
$json = '{"total":"100", "page":"1", "records":"100", "rows": [ 
{"no":"1","part_number":"2","part_name":"3","price":"4","test1":"5","test2":"7","note":"8"}]}';

请帮我。如何在 PHP 中的 JSON 中添加属性?

最佳答案

$json = '{"total":"100", "page":"1", "records":"100", "rows": [ 
{"no":"1","part_number":"2","part_name":"3","price":"4","note":"8"}]}';

// decode json
$json = json_decode($json);

// add data
$json->rows[0]->test1 = "5";
$json->rows[0]->test2 = "7";

// echo it for testing puproses
print_r($json);
// re-encode
$json = json_encode($json);

echo $json;

关于php - 如何在 PHP 中的 JSON 中添加属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17196256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com