【发布时间】:2014-08-21 14:14:37
【问题描述】:
这是我的问题:
我有一个 PHP 文件,它应该返回多条记录, 一切正常......但是,我不知道为什么,可能是因为某个地方的小错误......现在它不再工作了......
我的 Json_encode(my php) 返回这个:
{"1":{"id":"2222","name":"ERESRS"},"2":{"id":"1111","name":"LJLJM"}}
而不是简单地:
[{"id":"2222","name":"ERESRS"},{"id":"1111","name":"LJLJM"}]
以前有人遇到过这个问题吗?
我已经一次又一次地检查了我的 php 文件,但我没有找到这个“错误数组”的来源......
感谢帮助
这里是js代码:
$.ajax({
type : 'POST',
url : './php/getBenefListe.php',
data : {'id':idSoc},
error : function(){
alert('ERREUR MISE A JOUR DE LA LISTE');
},
success : function(response){
$("#benefListe").empty();
$('#benefListe').append($('<option>',{
value : '',
text : 'Choisissez dans la liste'
}));
alert("REPONSE : "+response);
var myData = JSON.parse(response);
for(var i=0;i<myData.length;i++){
var id = myData[i].id;
if(id-latestBenef > 0){
latestBenef = id;
}
var nom = myData[i].nom;
var prenom = myData[i].prenom;
var rue = myData[i].rue;
var numero = myData[i].num;
var boite = myData[i].bte;
var cp = myData[i].cp;
var loc = myData[i].loc;
if(rue!="" && numero!=""){
rue = rue+", "+numero;
}
if(cp!="" && loc!=""){
loc = "- "+cp+" "+loc;
}
var field = nom+" "+prenom+" ; "+rue+" "+boite+" "+loc;
$('#benefListe').append($('<option>',{
value : id,
text : field
}));
}
alert("B\351n\351ficiaire Ajout\351!");
$("#benefListe option[value="+latestBenef+"]").prop('selected',true);
$("#benefListe").change();
}
});
这里是php文件:
include "./functions.php";
if(isset($_POST['id']) && ($_POST['id']!='')){
$id = $_POST['id'];
$db = connectToDb('test');
$myArray = array();
$i = 0;
$getBenefIds = "SELECT DISTINCT IDPERSONNE FROM socrsp WHERE (IDSOCIETE = $id);";
$benefIds = $db->prepare($getBenefIds);
$benefIds->execute();
$count = $benefIds->rowCount();
if($count>0){
foreach($benefIds as $benefId){
$getBenef = "SELECT IDPERSONNE,NOM,PRENOM,ADRESSE,NUMERO,BTE,IDCOPOSTAL,CODEPAYS FROM personne WHERE IDPERSONNE = ".$benefId['IDPERSONNE'];
$myBenef = $db->prepare($getBenef);
$myBenef->execute();
foreach($myBenef as $benef){
if(!(is_numeric($benef['ADRESSE']))){
$myArray[$i]['id'] = $benef['IDPERSONNE'];
$myArray[$i]['nom'] = $benef['NOM'];
$myArray[$i]['prenom'] = $benef['PRENOM'];
$myArray[$i]['rue'] = '';
$myArray[$i]['num'] = '';
$myArray[$i]['rue'] = $benef['ADRESSE'];
$myArray[$i]['num'] = $benef['NUMERO'];
$myArray[$i]['bte'] = $benef['BTE'];
//RECUP CP ET LOCALITE
if((isset($benef['IDCOPOSTAL']) && ($benef['IDCOPOSTAL']!='0') && ($benef['IDCOPOSTAL']!='2913'))&&($benef['CODEPAYS']=="B")){
$whereQuery = "SELECT CODEPOSTAL,LIBLOCALITE FROM copostal WHERE IDCOPOSTAL = ".$benef['IDCOPOSTAL'];
$where = $db->prepare($whereQuery);
$where->execute();
foreach($where as $w){
$myArray[$i]['cp'] = $w['CODEPOSTAL'];
$myArray[$i]['loc'] = $w['LIBLOCALITE'];
}
}else if((isset($benef['IDCOPOSTAL']) && ($benef['IDCOPOSTAL']!='0'))&&($benef['CODEPAYS']!="B")){
$whereQuery = "SELECT CPEXTERNE,LOCEXTERNE FROM cpostext WHERE IDCPOSTEXT = ".$benef['IDCOPOSTAL'];
$where = $db->prepare($whereQuery);
$where->execute();
foreach($where as $w){
$myArray[$i]['cp'] = $w['CPEXTERNE'];
$myArray[$i]['loc'] = $w['LOCEXTERNE'];
}
}else{
$myArray[$i]['cp'] = '';
$myArray[$i]['loc'] = '';
}
$i++;
}else{
$i++;
}
}
}
}
echo json_encode($myArray);
$db = null;
}
【问题讨论】:
-
请发布您的 php 代码...我们是程序员,不是魔术师 xD
-
就是这样,魔术师 ;D
-
你试过
json_encode(array_values($array))吗? -
请发送
var_dump($myArray);的输出
标签: php jquery arrays json parsing