Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php更新JSON数组对象_Php_Arrays_Json_Laravel 5.3 - Fatal编程技术网

使用php更新JSON数组对象

使用php更新JSON数组对象,php,arrays,json,laravel-5.3,Php,Arrays,Json,Laravel 5.3,如何使用php动态更新以下JSON对象 这是我的json { "1": { "value0": { "id":0, "status":0, "quantity":"110" }, "value1":

如何使用php动态更新以下JSON对象

这是我的json

 {
    "1":
        {
            "value0":
                    {
                        "id":0,
                        "status":0,
                        "quantity":"110"
                    },
            "value1":
                    {
                        "id":1,
                        "status":1,
                        "quantity":"120"
                    }
        }
    "2":
        {
            value0":
                    {
                        "id":0,
                        "status":0,
                        "quantity":"132"
                    },
            "value1":
                    {
                        "id":1,
                        "status":1,
                        "quantity":"123"
                    },
        }
}
我想将值0的状态从键1更改为1。 如何实现这一点?

使用json\u decode()json\u enocde()

使用json\u decode()json\u enocde()


使用JSON_decode将JSON转换为数组。在数组中进行更新。然后使用JSON_encode将数组转换为JSON。$new_data=JSON_decode($data,true)$新的_数据[1][“值0”][“状态”]=1$data=json_encode($new_data);回波数据;噢,谢谢@manian,但是当我尝试json_endcode时,我的json变为
{“1”:{“value0”:{“status”:1}}}
。很抱歉,请检查我下面的答案。它将与您一样工作expected@JYoThI哦,谢谢,我真的很管用。使用JSON_解码将JSON转换为数组。在数组中进行更新。然后使用JSON_encode将数组转换为JSON。$new_data=JSON_decode($data,true)$新的_数据[1][“值0”][“状态”]=1$data=json_encode($new_data);回波数据;噢,谢谢@manian,但是当我尝试json_endcode时,我的json变为
{“1”:{“value0”:{“status”:1}}}
。很抱歉,请检查我下面的答案。它将与您一样工作expected@JYoThI哦,谢谢,我真的很管用。它不应该是
[“1”]
而不是
[1]
,因为它是一个assoc数组,[1]会触发第二个数组元素,哪个是键“2”?它不应该是
[“1”]
而不是
[1]
,因为它是一个关联数组,[1]会触发第二个数组元素,即键“2”?
$data = json_decode($data,true);  
$data["1"]["value0"]["status"]=1;   
$data = json_encode($data);
echo $data;