1

次のような配列があります。

{"calendar":{"date":"1","event":"1","description":"","code":"lab"}}

そして、この配列に新しい配列を入力したいのですが、カレンダー内でこの出力を実現したい:

{"calendar":
    {"date":"1","event":"1","description":"","code":"lab"}
    {"date":"2","event":"2","description":"","code":"lab"}
}

これはすべてフォーム投稿から発生しています。

<?php 
$dy_date = $_GET['dy_date'];
$dy_event = $_GET['dy_event'];
$dy_description = $_GET['dy_description'];
$dy_code = $_GET['dy_code'];

$calendar = array("calendar" => array("date" => $dy_date, "event" => $dy_event, "description" => $dy_description, "code"=> $dy_code));


$calendar_check = json_decode(file_get_contents('../calendar_dynamic.js'),true);

$updated_cal = array();
foreach($calendar_check as $data){
    $updated_cal["date"] = $dy_date;
    $updated_cal["event"] = $dy_event;
    $updated_cal["description"] = $dy_description;
    $updated_cal["code"] = $dy_code;
    $updated_cal = array_merge($calendar_check['calendar'], $updated_cal);
    $filename = "../calendar_dynamic.js";
    file_put_contents($filename, json_encode($updated_cal), FILE_APPEND);
}

?>

追加された配列を既存の配列の正しい場所にマージできないようです。

アイデア?

4

2 に答える 2

1

array_mergeの代わりにarray_push関数を使用します。これは、あなたの場合、array_mergeが、内部配列と新しい配列を組み合わせたuとして戻り配列の構造を変更するためです。

array_push ($calendar_check['calendar'], $updated_cal)
于 2013-03-21T02:09:11.690 に答える
1

これを試して

$filename = "../uc3_stats/calendar_dynamic.js";

$dy_date = $_GET['dy_date'];
$dy_event = $_GET['dy_event'];
$dy_description = $_GET['dy_description'];
$dy_code = $_GET['dy_code'];

$newentry_calendar = array("calendar" => array("date" => $dy_date, "event" => $dy_event, "description" => $dy_description, "code"=> $dy_code));

$old_calendar = json_decode(file_get_contents($filename),true);

$new_calendar = $old_calendar; //keep old entries 
$new_calendar['calendar'][] = $newentry_calendar['calendar']; //add new entry
file_put_contents($filename, json_encode($new_calendar)); // save to file

必要に応じてこれを短くすることもできますが、これは可能な限りコードに近いものです;)

于 2013-03-21T02:05:49.023 に答える