json 파일 배열질문입니다. 채택완료
Copy
$json_file = './data/recent.json';
$contents = file_get_contents($json_file);
$data = json_decode($contents,true);
[
{"line1":"12","line2":"18","line3":"20","date":"2020-06-23"},
{"line1":"28","line2":"32","line3":"27","date":"2020-06-22"},
{"line1":"20","line2":"32","line3":"21","date":"2020-06-21"}
]
$line1 = $data[0]['line1'];
$line2 = $data[0]['line2'];
$line3 = $data[0]['line3'];
$line4 = $data[0]['date'];
echo......
위내용으로 해서 맨 위 06-23일 내용만 가져오려고 합니다.
12, 8, 20, 2020-06-23
저렇게 짜는게 맞는지 모르겠네요..저 상태로 출력을 해보면...
맨위 06-23것만 출력이 되기는 하는데...
왠지 정확히 작성한거 같지는 않다는 느낌이 들어서 말이죠..
혹시 제가 빠뜨린 구문이 있나요?
조언 부탁드립니다.
답변 1개
채택된 답변
+20 포인트
답변에 대한 댓글 5개
5년 전
5년 전
=== recent.json 파일 ====
[
{"line1":"12","line2":"18","line3":"20","date":"2020-06-23"},
{"line1":"28","line2":"32","line3":"27","date":"2020-06-22"},
{"line1":"20","line2":"32","line3":"21","date":"2020-06-21"}
]
=== test.php ====
$json_file = './data/recent.json';
$contents = file_get_contents($json_file);
$data = json_decode($contents,true);
$line1 = $data[0]['line1']; ====> 12 가 출력되어야 함.
$line2 = $data[0]['line2'];
$line3 = $data[0]['line3'];
$line4 = $data[0]['date'];
이렇습니다.
[
{"line1":"12","line2":"18","line3":"20","date":"2020-06-23"},
{"line1":"28","line2":"32","line3":"27","date":"2020-06-22"},
{"line1":"20","line2":"32","line3":"21","date":"2020-06-21"}
]
=== test.php ====
$json_file = './data/recent.json';
$contents = file_get_contents($json_file);
$data = json_decode($contents,true);
$line1 = $data[0]['line1']; ====> 12 가 출력되어야 함.
$line2 = $data[0]['line2'];
$line3 = $data[0]['line3'];
$line4 = $data[0]['date'];
이렇습니다.
eyekiss
5년 전
보통 배열에 넣어서.. json_encode 를 사용해서 변환해서 사용합니다.
https://opentutorials.org/module/986/7091
직접 코딩할때는 아래 참조하세요.
https://dojang.io/mod/page/view.php?id=720
https://opentutorials.org/module/986/7091
직접 코딩할때는 아래 참조하세요.
https://dojang.io/mod/page/view.php?id=720
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인
로컬에서는 저리해도 되던데...호스팅에 올려보니 안되네요..ㅎㅎ