我正在尝试在 jquery 中使用 getJSON 方法来检索动态数组。我在尝试对动态数组进行编码时遇到“非法”偏移错误。这是服务器端代码(我相信 javascript 是正确的,因为当我删除查询时它运行良好):
<?php
session_start();
require_once "database.php";
db_connect();
require_once "auth.php";
$current_user = current_user();
include_once("config.php");
$westcoast_id = $_GET['westcoast_id'];
$westcoast_array = array();
$query = "SELECT city, state FROM users GROUP BY city";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
if($row['city'] != '' && $row['state'] != '') {
$westcoast_array[$row] = "location:".$row['city'].", ".$row['state'].", stopover:true";
}
}
$data = array($westcoast_id, $westcoast_array);
echo json_encode($data);
?>
非法偏移量参考以下行:
$westcoast_array[$row] = "location:".$row['city'].", ".$row['state'].", stopover:true";
我看不出问题是什么。感谢您的帮助!