PHP多维数组我有这个PHP接收在阿贾克斯
include_once($preUrl . "openDatabase.php");
$sql = 'SELECT * FROM dish';
$query = mysqli_query($con,$sql);
$nRows = mysqli_num_rows($query);
if($nRows > 0){
$dishes = array();
while($row = $query->fetch_assoc()) {
$dishes[] = $row;
}
}else{
$dishes = "cyke";
}
echo json_encode($dishes , JSON_FORCE_OBJECT);
这阿贾克斯(在framework7)
myApp.onPageInit('dailyMenu',function() {
$$.post('http://theIP/eatsServer/dailyMenu.php', {}, function (data) {
console.log(data);
});
});
我在阿贾克斯的数据得到的是
{ “0” :{ “IDC”: “2”, “标题”: “helloWorld1”, “副标题”: “hellsubWorld”, “价格”: “16.5”, “IMG”: “IMG/testeImg.jpg”, “索多特”: “0”}, “1”:{ “IDC”: “3”, “标题”: “helloworld2”, “副标题”: “hellosubWorld2”, “价格”: “20.5”, “IMG”:“IMG/testeImg .jpg“,”soldout“:”1“}}
我已经试过数据了[0];数据[0]。 data.0; data0当我使用数据[“0”]只是给了我'''。
我想获得标题和其余的内部0.做一个cicle的地方,我将打印多个div,我只改变数组在html页面中的位置。
为例
for(...){
innerhtml += <div clas="">
<div class""> data(position i).title </div>
<div> data(position i) subtitle</div>
</div>
}
我已经尝试过,但每当我把例如:var obj = jQuery.parseJSON(json_data);但alert或consoleLog不起作用 – sergio
用法:var obj = JSON.parse(data); ('http://theIP/eatsServer/dailyMenu.php',{},function(data){ var obj = JSON.parse(data); console.log(obj) ; },'json'); –
我把你在commentatys中说过的东西,但那不是在答案,所以我标记你anwser正确或什么?在这里,如果你需要编辑我所提出的$$ post('url',{},function(data ){ var obj = JSON.parse(data); console.log(obj); alert(obj [“1”]。title); });注:感谢它的工作;) – sergio