2013-03-21 4 views
1

I는 다음과 같습니다 배열이 있습니다삽입 배열

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

을 그리고 난이 출력 달성하기 위해 입력이 배열에 있지만 일정 안에 새로운 배열을 원하는 :

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

을 이 모든 것은 양식 게시판에서 발생합니다.

기존 배열의 올바른 위치에 추가 된 배열을 병합하는 것처럼 보이지 않습니다.

아이디어가 있으십니까?

답변

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 

는이를 단축 할 수하려고하지만,이 가능한 코드에 가깝다) 내가 대신 array_merge의 array_push 기능을 사용

+0

감사합니다. – JMP

+0

당신을 진심으로 환영합니다. – itsid

1

때문에에 케이스 array_merge 유 새로운 어레이 내 배열 등을 조합 한 리턴 어레이의 구조를 변경한다.

array_push ($calendar_check['calendar'], $updated_cal)