【发布时间】:2017-01-13 02:32:34
【问题描述】:
我正在尝试创建一个数据库系统来处理保存的 fabricjs 画布。为了让每件事都能正常工作,我需要画布对象
json=canvas.toDatalessJSON; // in javascript
...在 php 中保存为索引数组系统中的 .json 文件,允许我加载和添加更多画布对象或删除对象。 困扰我的是如何实际构建它。我在这里尝试的是:
... The json structure i'm considering ...
["1"]{Object:.....}
["2"]{Object:.....}
["4"]{Object:.....} (["3"] deleted in this case)
... Trying to create in php ...
$i="1";
$A=[];
$A[$i]=[];
array_push($A[$i],json_decode($json));
$newjson=json_encode($A);
但这显然不是实现它的方法。有什么想法吗?
编辑 1 感谢您的回答@Michael。我最终没有完全使用您的建议。但我接受它作为让我找到解决方案的答案。在我的解决方案中,我将索引添加到对象,并在我要使用对象时取消设置索引。
创建:
$dataA=array();
$dataB=json_decode($data,true);
$dataB["index"]= $ThumbIndex;
$dataA[]=&$dataB;
$data=json_encode($dataA);
file_put_contents($filename.'.json',$data);
添加:
$data0=file_get_contents($filename.'.json');
$dataA=json_decode($data0,true);
$dataB=json_decode($data,true);
$dataB["index"]= $ThumbIndex;
$dataA[]=&$dataB;
$data=json_encode($dataA);
file_put_contents($filename.'.json',$data);
【问题讨论】:
标签: php arrays json canvas fabricjs