0

我有 json_encoded 数组,我想添加到使用 php

[{"id":"a","value":"2"},{"id":"b","value":"2"}]

我想将以下内容添加到上面的数组中:

array("id" => c, "value" => "3") 

我尝试json_decode然后尝试将数组推入其中,但我对如何做到这一点感到困惑

4

1 回答 1

1

确保在数组模式而不是对象模式下使用json_decode :

// Default: JSON is decoded as object
$json_object = json_decode($json_string);

// Pass true in the second argument to get an array instead
$json_array = json_decode($json_string, true);

// Push a new entry onto the end
$json_array[] = array("id" => c, "value" => "3");

// Re-encode JSON string, if needed
$json_final_string = json_encode($json_array);
于 2012-04-24T22:10:05.147 回答