我正在使用一个想要在javascript中输入这样的数组的插件:
var data = [
{
"id": 1,
"name": "University1",
"list": [
{"id": 1, "name": "Dorms", "list":
[
{"id": 1, "name": "Dorm1"},
{"id": 2, "name": "Dorm2"}
]
},
{"id": 2, "name": "Off-Campus", "list":
[
{"id": 1, "name": "North Campus"},
{"id": 2, "name": "East Campus"}
]
}
]
},
{
"id": 2,
"name": "University2",
"list": [
{"id": 1, "name": "Dorms", "list":
[
{"id": 1, "name": "Dorm1"},
{"id": 2, "name": "Dorm2"}
]
},
{"id": 2, "name": "Off-Campus", "list":
[
{"id": 1, "name": "North Campus"},
{"id": 2, "name": "East Campus"}
]
}
]
}
];
我的数组数据在SQL数据库中。我在PHP中形成这个多维数组和/或用AJAX传递它时遇到了麻烦。
我的javascript / jquery:
var locationsArray;
$.post(
'ajax/locationDropdown.php',
{
//NO DATA THIS TIME
},
function (response) {
console.log(response);
parseResponse = $.parseJSON(response);
var locationsArray = $.map(parseResponse, function(value, index) {
return [value];
});
console.log(locationsArray);
}
);
我的php:
<?php
include 'databaseConnection.php';
$sqlLD1 = '
SELECT DISTINCT school
FROM timeBlocks
ORDER BY school ASC;
';
if (!$resultLD1 = $connection->query($sqlLD1)) {
die ('There was an error running the queryLD1 [' . $connection->error . ']');
}
$locationArray = array(
'id'=>array(),
'name'=>array(),
'list'=>array(
'id'=>array(),
'name'=>array(),
'list'=>array(
'id'=>array(),
'name'=>array()
)
)
);
$i=0;
while ($rowLD1 = $resultLD1->fetch_assoc()) {
$school = $rowLD1["school"];
$locationArray[$i][name] = $school;
$sqlLD2 = '
SELECT DISTINCT timeBlockLocation
FROM timeBlocks
WHERE school = "'.$rowLD1["school"].'"
ORDER BY timeBlockLocation ASC;
';
if (!$resultLD2 = $connection->query($sqlLD2)) {
die ('There was an error running the queryLD2 [' . $connection->error . ']');
}
$j=0;
while ($rowLD2 = $resultLD2->fetch_assoc()) {
$timeBlockLocation = $rowLD2["timeBlockLocation"];
$locationArray[$i][$j][name]=$timeBlockLocation;
$sqlLD3 = '
SELECT DISTINCT timeBlockSubLocation
FROM timeBlocks
WHERE school = "'.$rowLD1["school"].'"
AND timeBlockLocation = "'.$rowLD2["timeBlockLocation"].'"
ORDER BY timeBlockSubLocation ASC;
';
if (!$resultLD3 = $connection->query($sqlLD3)) {
die ('There was an error running the queryLD2 [' . $connection->error . ']');
}
$k=0;
while ($rowLD3 = $resultLD3->fetch_assoc()) {
$timeBlockSubLocation = $rowLD3["timeBlockSubLocation"];
$locationArray[$i][$j][$k][name]=$timeBlockSubLocation;
$k++;
}
$j++;
}
$i++;
}
echo json_encode($locationArray);
&GT;
这导致一个如下所示的数组:
{
"0": {
"0": {
"0": {
"name": "All Locations"
},
"name": "Off Campus"
},
"1": {
"0": {
"name": "Dorm1"
},
"1": {
"name": "Dorm2"
}
"name": "Dorms"
},
"name": "University1"
},
"1": {
"0": {
"0": {
"name": "All Locations"
},
"name": "Off-Campus"
},
"1": {
"0": {
"name": "Dorm1"
},
"name": "Dorms"
}
"name": "University2"
},
"id": [],
"name": [],
"list": {
"id": [],
"name": [],
"list": {
"id": [],
"name": []
}
}
}
答案 0 :(得分:1)
我不会完全重写这一点,而是指出一种更清晰的通用方法
删除$locationArray
声明中的所有子数组,然后执行
$locationArray = array();
然后在外部循环中为每次迭代启动一个新数组。在嵌套循环中,更新数组在外部循环内部开始,然后在外部循环更新主输出数组
的末尾while ($rowLD1 = $resultLD1->fetch_assoc()) {
$school = array(
'id' => $rowLD1["id"],
'name'=> $rowLD1["school"],
'list' => array()
);
// school query
while ($rowLD2 = $resultLD2->fetch_assoc()) {
// add to current school list array
$school['list'][] = array(
'prop1' => $rowLD2['prop1'],
'prop2' => $rowLD2['prop2']
);
}
// now add $school to main array
$locationArray[] = $school;
}
echo json_encode....
答案 1 :(得分:1)
从性能角度来看,最好避免在嵌套循环中执行查询。通常您可以通过将表连接在一起来实现此目的幸运的是,您的所有结果都来自同一张桌子,因此您甚至不必这样做。您只需运行一个简单的查询:
$sql = 'SELECT school, timeBlockLocation, timeBlockSubLocation
FROM timeBlocks
ORDER BY school, timeBlockLocation, timeBlockSubLocation';
$query_result = $connection->query($sql);
然后使用它们的值作为数组键获取结果:
while ($row = $query_result->fetch_assoc()) {
$schools[$row['school']][$row['timeBlockLocation']][$row['timeBlockSubLocation']] = 1;
// (1 is meaningless, just a placeholder)
}
使用查询结果中的值作为数组键,可以轻松创建所需的结构并防止重复输入。 这将为您提供如下数组:
$schools = [
'University1' => [
'Dorms' => ['Dorm1' => 1, 'Dorm2' => 1],
'Off-Campus' => ['East Campus' => 1, 'North Campus' => 1]
],
'University2' => [
'Dorms' => ['Dorm1' => 1, 'Dorm2' => 1],
'Off-Campus' => ['East Campus' => 1, 'North Campus' => 1]
]
];
因为您需要的最终结果的每个级别具有相同的格式,所以递归函数可以很好地将中间数组转换为该格式:
function reformat($array) {
$id = 1;
foreach ($array as $key => $value) {
$branch = ['id' => $id++, 'name' => $key];
// recursively reformat each level
if (is_array($value)) $branch['list'] = reformat($value);
$branches[] = $branch;
}
return $branches;
}
$locationArray = reformat($schools);
答案 2 :(得分:0)
在每一步声明数组,然后填充它。在这种情况下,请勿使用计数器$i, $j
推送值
$arr1 = array(
'id': <id>,
...
'list': array()
);
while(<condition>){
$arr1['list'][] = array(
'id' => <list_id>
...
);
}
...