在PHP中将JSON模式复制并添加为数组

时间:2016-04-13 13:55:48

标签: php arrays json

我正在尝试使用php将密钥及其内容的副本添加到JSON文件中。

$content = file_get_contents($_POST["content"]);
$decode = json_decode($content,true);
$version = $_POST["version"];
array_unshift($decode['versions'],$version );
$encode = json_encode($decode,true);

我们想说我们要添加版本1.2.2。这增加了:

...["versions"]=> array(6) { [0]=> string(5) "1.2.2" ["1.2.1"]=> array(4) { ["project"]=> string(21)...

而不是创建1.2.2密钥及其子数组。

也尝试过:

array_unshift($decode['versions'][$version],$version );

没有结果

1 个答案:

答案 0 :(得分:1)

如果我没有正确查看,您可以直接将版本放入您的内容中,如下所示:

$content = file_get_contents($_POST['content']);
$decode = json_decode($content, true);

$version = $_POST['version'];
$decode['version'] = $version;

$encode = json_encode($decode, true);