我从 php/mysql 数据库查询中获取了一个 json 多维数组(作为 $.ajax 调用的成功函数中的“数据”)。PHP 脚本将其发送到 javascript 文件,如下所示:
header('Content-Type: application/json');
echo json_encode($arr);
查询可以从数据库返回一条或多条记录。
console.log(data) 似乎只给了我“父”数组中的第一个“子”数组。这是控制台中的内容:
[{id:114, branchStateCovered:MN, branchCountyCovered:Aitkin,…},...]
0: {id:114, branchStateCovered:MN, branchCountyCovered:Aitkin,…}
1: {id:115, branchStateCovered:MN, branchCountyCovered:Benton,…}
2: {id:116, branchStateCovered:MN, branchCountyCovered:Carlton,…}
3: {id:117, branchStateCovered:MN, branchCountyCovered:Chisago,…}
4: {id:118, branchStateCovered:MN, branchCountyCovered:Cook,…}
5: {id:119, branchStateCovered:MN, branchCountyCovered:Crow Wing,…}
6: {id:120, branchStateCovered:MN, branchCountyCovered:Isanti,…}
7: {id:121, branchStateCovered:MN, branchCountyCovered:Itasca,…}
8: {id:122, branchStateCovered:MN, branchCountyCovered:Kanabec, branchZipCodesCovered:56358, 55051}
9: {id:123, branchStateCovered:MN, branchCountyCovered:Lake,…}
10: {id:124, branchStateCovered:MN, branchCountyCovered:Mille Lacs,…}
11: {id:125, branchStateCovered:MN, branchCountyCovered:Pine,…}
12: {id:126, branchStateCovered:MN, branchCountyCovered:Saint Louis,…}
13: {id:127, branchStateCovered:WI, branchCountyCovered:Douglas,…}
在不同的 $.ajax 调用中,我正在访问始终是一维数组的内容
$('label#branchName').text(data['name']);
$('label#branchAddress').text(data['address']);
etc...
但在这种情况下,我需要遍历每个数组并以与上述类似的方式显示其每个值。
我找到了这个 SO post,但看起来帖子作者正在创建数组,因为他知道每个“子”数组的“名称”(生产者)。也许对我来说答案就在那篇文章中,而我只是没有看到。
如何获取输出的多维数组并循环遍历它以将每个数组的数组显示到一个表中 - 或者我想在 HTML 端用它做什么?