I insert several(array
) value with json_encode
in one row from database table, now want echo they as order with jquery.
我将json_encode的几个(数组)值从数据库表中插入到一行中,现在希望以jquery的顺序对它们进行回显。
This is output from my PHP code:
这是我的PHP代码的输出:
[{
"guide": null,
"residence": [{
"name_r": "jack"
}, {
"name_r": "jim"
}, {
"name_r": "sara"
}],
"residence_u": [{
"units": ["hello", "how", "what"],
"extra": ["11", "22", "33"],
"price": ["1,111,111", "2,222,222", "3,333,333"]
}, {
"units": ["fine"],
"extra": ["44"],
"price": ["4,444,444"]
}, {
"units": ["thanks", "good"],
"extra": ["55", "66"],
"price": ["5,555,555", "6,666,666"]
}]
}]
I want as(output):
我想要(输出):
jack
hello
&11
&1,111,111
how
&22
&2,222,222
what
&33
&3,333,333,
jack hello & 11 & 1,111,111 how & 22 & 222,222 what & 33 & 333,333,
jim
fine
&44
&4,444,444
jim fine,44和4444444
sara
thanks
&55
&5,555,555
good
&66
&6,666,666
萨拉,谢谢,55和555555555,好,66和6666666666
How is it?
它是如何?
1 个解决方案
#1
2
Assuming
假设
- you already know how make the ajax request that fetches this information from your PHP script
- 您已经知道如何从PHP脚本中获取这些信息的ajax请求
- there is always one element in the top-level array (if not, you could add one more level of iteration)
- *数组中总是有一个元素(如果没有,您可以再添加一个级别的迭代)
- by output you mean access the corresponding elements - I've put in calls to
console.log
but you couldalert()
or put into a DOM element or whatever - 通过输出,您的意思是访问相应的元素——我已经调用了控制台。可以使用alert()或将其放入DOM元素或其他内容
you could do something like this in jQuery (here's an example fiddle without the network part. You'll see the output in your console)
您可以在jQuery中做类似的事情(这里有一个示例小提琴,没有网络部分。您将在控制台中看到输出)
var data = response[0]; //response is the data received by the jQuery ajax success callback
var residences = data.residence;
var residence_u = data.residence_u;
$.each(residences, function(index, val){
var name = val.name_r;
console.log(name);
var info = residence_u[index]; //get the corresponding residence_u element
$.each(info.units, function(index, val){
var unit = val;
var extra = info.extra[index];
var price = info.price[index];
console.log( val + " & " + extra + " & " + price);
});
});
#1
2
Assuming
假设
- you already know how make the ajax request that fetches this information from your PHP script
- 您已经知道如何从PHP脚本中获取这些信息的ajax请求
- there is always one element in the top-level array (if not, you could add one more level of iteration)
- *数组中总是有一个元素(如果没有,您可以再添加一个级别的迭代)
- by output you mean access the corresponding elements - I've put in calls to
console.log
but you couldalert()
or put into a DOM element or whatever - 通过输出,您的意思是访问相应的元素——我已经调用了控制台。可以使用alert()或将其放入DOM元素或其他内容
you could do something like this in jQuery (here's an example fiddle without the network part. You'll see the output in your console)
您可以在jQuery中做类似的事情(这里有一个示例小提琴,没有网络部分。您将在控制台中看到输出)
var data = response[0]; //response is the data received by the jQuery ajax success callback
var residences = data.residence;
var residence_u = data.residence_u;
$.each(residences, function(index, val){
var name = val.name_r;
console.log(name);
var info = residence_u[index]; //get the corresponding residence_u element
$.each(info.units, function(index, val){
var unit = val;
var extra = info.extra[index];
var price = info.price[index];
console.log( val + " & " + extra + " & " + price);
});
});