2016-09-05 4 views
2

저장된 fabricjs 캔버스를 처리하기 위해 데이터베이스 시스템을 만들려고합니다. 모든 일이 작동하려면 내가 캔버스 나로드와 개체를 더 캔버스 객체를 추가하거나 삭제할 수있는 인덱싱 된 배열 시스템에 .json 파일로 PHP에 저장할 수 ... json 형식으로 fabricjs-canvas 객체를 PHP의 인덱스 배열에 추가하려면 어떻게해야합니까?

json=canvas.toDatalessJSON; // in javascript 

객체가 필요합니다. 나를 괴롭히는 것은 실제로 이것을 구조화하는 방법입니다. 내가 여기서하려고하는 것은 다음과 같습니다 :

... 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); 

답변

2

당신이 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); 
관련 문제