Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用奇怪的JSON格式的AJAX响应_Jquery_Json - Fatal编程技术网

Jquery 使用奇怪的JSON格式的AJAX响应

Jquery 使用奇怪的JSON格式的AJAX响应,jquery,json,Jquery,Json,您好,我有一个PHP脚本,它从一个表中查找用户的所有电话号码,然后根据电话号码列表在另一个表中查找他的信息 PHP脚本: $intUserID = $_POST['intUserID_PHP']; //This is the user ID $arrayUserPhoneNumbers = $_POST['arrayUserPhoneNumbers'];//this is an array of all the user's phone numbers. try {

您好,我有一个PHP脚本,它从一个表中查找用户的所有电话号码,然后根据电话号码列表在另一个表中查找他的信息

PHP脚本:

$intUserID = $_POST['intUserID_PHP']; //This is the user ID 
$arrayUserPhoneNumbers = $_POST['arrayUserPhoneNumbers'];//this is an array of all the user's phone numbers.
  try {     
        $DBC  = new PDO("pgsql:host=$host;port=$port;dbname=$dbname;user=$user;           password=$password");//All my DB connection is well set.
        $query1 = "SELECT phones FROM usersphones WHERE id=".$intUserID;
        $sth = $DBC->prepare($query1);
        $sth->execute();
        $result = $sth->fetchAll();
        $i = 0;
           foreach ($result as $data) {
             $query2 = "SELECT txtshare,dtzserver,adress,issue FROM tbluserinfo WHERE  phone='".$data['phones']."'";
             $sth = $DBC->prepare($query2);
             $sth->execute();
             $result2 = $sth->fetchAll();
             echo json_encode($result2);
             $i++;  
           }    
}           
catch(PDOException $e) {
     echo 'Error';   
}
这是我正在使用的JQuery代码:

$.ajax({
    type: "POST",
    url: "getinfo.php",              
    dataType:'json',
    data: {arrayUserPhoneNumbers : arrayUserPhoneNumbers,intUserID : intUserID},
success: function(data) {
}
});
我的问题是: 我得到了很多行,这只是最后一行,我从firebug控制台得到的JSON结果是:

[
    {
        "txtshare": "F",
        "0": "F",
        "dtzserver": "2013-01-05 00:32:55.311037+00",
        "1": "2013-01-05 00:32:55.311037+00",
        "phone": "+33522988655",
        "2": "+33522988655",
        "issue": "Lost my smartphone",
        "3": "Lost my smartphone"
    }
]
我的JSON是有效的,但我不知道为什么在这个结果中会出现“0”、“1”、“2”和“3”索引,其中包含重复的数据?在我的表中,我只有txtshare、dtzserver、phone和issue字段。 我希望它是这样的:

[
        {
            "txtshare": "F",
            "dtzserver": "2013-01-05 00:32:55.311037+00",
            "phone": "+33522988655",
            "issue": "Lost my smartphone",
        }
 ]

提前感谢。

您可能会看到返回的JSON同时包含key=>值对和数组索引格式,因此您可以从中选择任意1。 访问:

输出:

序列阵列 阵列(4){ [0]=> 字符串(3)“foo” [1]=> 字符串(3)“bar” [2]=> 字符串(3)“baz” [3]=> 字符串(5)“blong” } 字符串(27)“[“foo”,“bar”,“baz”,“blong”]

非顺序阵列 阵列(4){ [1]=> 字符串(3)“foo” [2]=> 字符串(3)“bar” [3]=> 字符串(3)“baz” [4]=> 字符串(5)“blong” } 字符串(43)“{”1:“foo”,“2:“bar”,“3:“baz”,“4:“blong”}”

单键未设置的序列数组 阵列(3){ [0]=> 字符串(3)“foo” [2]=> 字符串(3)“baz” [3]=> 字符串(5)“blong” } 字符串(33)“{”0:“foo”,“2:“baz”,“3:“blong”}”

echo "Sequential array".PHP_EOL;
$sequential = array("foo", "bar", "baz", "blong");
var_dump(
 $sequential,
 json_encode($sequential)
);

echo PHP_EOL."Non-sequential array".PHP_EOL;
$nonsequential = array(1=>"foo", 2=>"bar", 3=>"baz", 4=>"blong");
var_dump(
 $nonsequential,
 json_encode($nonsequential)
);

echo PHP_EOL."Sequential array with one key unset".PHP_EOL;
unset($sequential[1]);
var_dump(
 $sequential,
 json_encode($sequential)
);