如何将以下类型的数组转换为PHP中的Json以及如何从ajax获取它
array(4) {
["START_TIME"]=>
string(19) "2017-12-19 08:34:01"
["END_TIME"]=>
string(19) "2017-12-19 10:34:07"
["DESCRIPTION"]=>
string(30) "MORNING TEA (AFTER BREAKFAST )"
["INTERVAL_ID"]=>
string(1) "2"
}
这是我得到的json [{“START_TIME”:“2017-12-19 09:31:59”,“END_TIME”:“2017-12-19 10:44:04”,“DESCRIPTION”:“BREAKFAST”, “INTERVAL_ID”:“1”},{“START_TIME”:“2017-12-19 08:34:01”,“END_TIME”:“2017-12-19 10:34:07”,“说明”:“早上好茶(早餐后)“,”INTERVAL_ID“:”2“}]如何在ajax成功功能上展示它
echo json_encode($arr)
检查这个json_encode
您可以使用json_encode
将数组编码为JSON。
http://php.net/manual/en/function.json-encode.php
以下页面可以帮助您理解AJAX和PHP
https://www.w3schools.com/xml/ajax_php.asp
请注意,当您从PHP返回JSON对象时,您必须使用JSON.parse
在客户端解析它
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
希望这可以帮助。
在Php
$array = [
['arr1'=>'test1'],
['arr2'=>'test2'],
]
echo json_encode($array);
并在Jquery ajax上
<script>
$.getJSON("URL",function(response){
console.log(response)
})
})
</script>