在JavaScript中将PHP数组转换为JSON数组时,“,”成为数组的第一个元素

时间:2016-11-17 11:14:38

标签: javascript php arrays json

当我将PHP数组转换为JSON数组时,数组的第一个元素为空,它是“,”。 这是我的代码:

XMLHttpRequest cannot load http://localhost:8000/da. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

我已经在PHP中检查了数组是否可以,但是当我转换它时会发生这种情况。

1 个答案:

答案 0 :(得分:1)

以下是解决方案:

<?php echo json_encode(array_values(array_filter($d_dates)));?>

array_filter删除数组中的所有空元素