1
test_bots.json에서 값의 설정을 해제하고 다시 저장하려고 시도하지만 데이터 형식이 프로세스에서 변경되고 있습니다.내 JSON 배열이 객체로 변환되는 이유는 무엇입니까?
test_bots.json이 JSON 배열을 포함
내 코드는 다음과 같습니다["John","Vladimir","Toni","Joshua","Jessica"]
:
$good = 'Toni';
$good_arr = file_get_contents('test_bots.json');
$good_arr = json_decode($good_arr);
if(in_array($good, $good_arr)){
$key = array_search($good, $good_arr);
unset($good_arr[$key]);
$good_arr2 = json_encode($good_arr);
file_put_contents('test_bots.json',$good_arr2);
}
저장되는 출력은 다음과 같습니다
{"0":"John","1":"Vladimir","3":"Joshua","4":"Jessica"}
하지만 출력을 원하는 모양 :
["John","Vladimir","Joshua","Jessica"]
저장하기 전에 배열을 비 직렬화하려고했지만 작동하지 않습니다.
왜 이런 일이 발생합니까?
'file_put_contents'에서'$ good_arr'을'$ good_arr2'로 변경하십시오. – jardis
그냥 오타처럼 보입니다. @jardis가 지적했듯이,'json_encode'로 작성한 변수를 파일에 저장하지 않으면 인코딩되지 않은 배열이 저장됩니다. –
죄송합니다. 오타되었습니다. 나는 나의 질문을 편집했다. @ Do not Panic – MHH