【发布时间】:2014-11-17 05:14:57
【问题描述】:
我正在尝试获取 Android 应用程序发布的 JSON,然后将 JSON 解码为一个数组,但得到:未定义索引:第 6 行的 usersJSON []
//Get JSON posted by Android Application
$json = $_POST["usersJSON"]; // Undefined index
php 脚本如下所示:
<?php
include_once './db_functions.php';
//Create Object for DB_Functions clas
$db = new DB_Functions();
//Get JSON posted by Android Application
$json = $_POST["usersJSON"]; // Undefined index
//Remove Slashes
if (get_magic_quotes_gpc()){
$json = stripslashes($json);
}
//Decode JSON into an Array
$data = json_decode($json);
//Util arrays to create response JSON
$a=array();
$b=array();
//Loop through an Array and insert data read from JSON into MySQL DB
for($i=0; $i<count($data) ; $i++)
{
//Store User into MySQL DB
$res = $db->storeUser($data[$i]->userId,$data[$i]->userName);
//Based on inserttion, create JSON response
if($res){
$b["id"] = $data[$i]->userId;
$b["status"] = 'yes';
array_push($a,$b);
}else{
$b["id"] = $data[$i]->userId;
$b["status"] = 'no';
array_push($a,$b);
}
}
//Post JSON response back to Android Application
echo json_encode($a);
?>
【问题讨论】:
-
未定义的索引意味着您没有该索引的值
标签: php android mysql arrays json