1
이 작업을 통해 js 객체를 양식으로 만든 다음이 데이터를 json 파일에 저장합니다. 내가 지금 가지고있는 것은이 작업을 수행하고 파일이 이미 존재하는 경우 파일에 새 데이터를 추가하는 코드입니다. 유일한 문제는 데이터가 배열로 추가되지만 한 주 배열에 객체로 추가해야합니다. 자바 스크립트 객체를 php로 json 파일에 추가
이
는 나의 PHP 지식이 매우 제한되어 있고 난 내 길을 인터넷 검색을하고있어 출력 (예)[
[
{
"campaignId": "campaign1",
"id": "c1"
}
],
[
{
"campaignId": "campaign2",
"id": "c2"
}
]
]
하지만 내가 필요로하는 것은
[
{
"campaignId": "campaign1",
"id": "c1"
},
{
"campaignId": "campaign2",
"id": "c2"
}
]
나는 PHP 개발자 아니다이다 이 작업을 통해하지만 나는 구글이 실패하고있는 지점으로 왔습니다. 여기
내가이 문제를 해결하는 방법에 대한 예리한이고 더 중요한 것은 원하는 출력을 얻기 위해이 문제를 해결하는 방법을 이해<?php
$json = $_POST['json'];
$name = $_POST['name'];
$cat = $_POST['category'];
// make requested directory
// to see if directory exists
$filename = "savedData/$cat/";
if (file_exists($filename)) {
//echo "The directory {$dirname} exists";
} else {
mkdir($filename, 0777);
echo "The directory {$dirname} was successfully created.";
}
$file = "savedData/$cat/$name.json";
// Append new form data in json string saved in text file
$json = json_decode($json);
$formdata = array(
$json
);
$arr_data = array(); // to store all form data
// check if the file exists
if(file_exists($file)) {
// gets json-data from file
$jsondata = file_get_contents($file);
// converts json string into array
$arr_data = json_decode($jsondata, true);
}
// appends the array with new form data
$arr_data[] = $formdata;
// encodes the array into a string in JSON format (JSON_PRETTY_PRINT - uses whitespace in json-string, for human readable)
$jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);
// saves the json string in $file
// outputs error message if data cannot be saved
if(file_put_contents($file, $jsondata)) echo 'Data successfully saved';
?>
내 PHP 코드, 어떤 도움이 좋지 않을까 :)
그게 멋진, 그래서 간단! :) 감사합니다, 그 쉬운 방법을 아는 때 – iamleeadamson