【问题标题】:How do i add fabricjs-canvas objects in json format to a indexed array in php?如何将 json 格式的 fabricjs-canvas 对象添加到 php 中的索引数组?
【发布时间】: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


    【解决方案1】:

    假设您正在尝试使用 php 覆盖 json 对象中的元素“1”,请尝试以下操作:

    $newjson = json_decode($json, true);    
    $newjson["1"] = Array();    
    $newjson = json_encode($newjson);
    

    如果要添加元素:

    $newjson = json_decode($json, true);    
    $newjson[] = Array();
    $newjson = json_encode($newjson);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 2017-11-14
      • 2020-07-27
      • 2012-11-16
      • 2020-02-22
      • 2011-11-21
      相关资源
      最近更新 更多