2

我正在尝试在 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";

我看不出问题是什么。感谢您的帮助!

4

2 回答 2

1
 $westcoast_array[$row] = [...]

是错的。(您不能使用数组作为键)

只需使用:

$westcoast_array[] = [...]
于 2013-03-01T21:39:14.237 回答
0

尝试使用:

if($row['city'] != '' && $row['state'] != '') {
    $westcoast_array[] = "location:".$row['city'].", ".$row['state'].", stopover:true";
}

或者

if($row['city'] != '' && $row['state'] != '') {
    $westcoast_array[$row['state']][$row['city']] = "location:".$row['city'].", ".$row['state'].", stopover:true";
}

然后你可以用它var_dump($westcoast_array)来查看结构。

于 2013-03-01T21:45:10.847 回答