【发布时间】:2016-01-07 16:18:16
【问题描述】:
所以我有一个 select 语句,它从 DB 中返回 3 个值:
- 文件夹 ID,
- 文件夹名称,
- 家长 ID。
如果文件夹没有父文件夹,则返回 null 作为父 ID。
使用这些数据,我将结果拆分为 2 个数组。
一个名为$tree,将用作“目录树”,首先将包含所有父ID设置为空的父文件夹...
另一个名为$children,其中包含所有其他子文件夹。
通过这些,我试图创建一个多维array($tree),我将使用它向使用 PHP/HTML 的用户显示文件结构。理想情况下,这将使用递归来允许不知道目录实际有多深。
我目前正在尝试以下操作,但没有成功,经过一整天的思考,我感到卡住了(函数 find Children 被函数 getDirTree 进一步调用):
// should be recursive function. Takes 2 arrays as an argument.
// returns $tree() which will be a multiple dimension array
function findChildren($tree, $children){
// tree has 2 parents in first run
foreach($tree as $folder){
$tempArray = array();
// children has 4
foreach($children as $child){
if ($child['parentId'] === $folder['folderId']){
array_push($tempArray, $child);
if(($childKey = array_search($child, $children)) !== false) {
unset($children[$childKey]);
}
}
}
if(($parentKey = array_search($tree, $folder)) !== false) {
array_push($children[$parentKey],$tempArray);
}
}
// Need to have some sort of recursion in this function
// if (!empty($children)){
// findChildren($tree, $children);
// }
}
// takes userId as int and returns a multi dimensional array representing the users folder structure.
function getDirTree($userId){
global $mysqli;
$children = array();
$tree = array();
if($folders = $mysqli->prepare("SELECT folders.id, folders.name, child_of_folder.parent_id
FROM child_of_folder
RIGHT JOIN folders
ON (child_of_folder.child_id = Folders.id)
WHERE folders.user_id = ?;")) {
// Bind the parameters... s for String and the variable $name to be bound.
if ($folders->bind_param("i", $userId)) {
// execute the query
if ($folders->execute()) {
// store the results
if($folders->store_result()){
// bind the results
if($folders->bind_result($folderId, $folderName, $parentId)) {
// Fetch the results
while ($folders->fetch()) {
if ($parentId === null) {
array_push($tree, array('folderId' => $folderId, 'folderName' => $folderName, 'parentId' => $parentId));
} else {
array_push($children, array('folderId' => $folderId, 'folderName' => $folderName, 'parentId' => $parentId));
}
}
} else {
$hasFolder = null;
}
} else {
// if there were no values to store return false
$hasFolder = null;
}
} else {
// if there was a problem executing the statement return null
$hasFolder = null;
}
} else {
// if there was a problem binding the statement return null
$hasFolder = null;
}
} else {
// if there was a problem preparing the statement return null
$hasFolder = null;
}
if(!empty($children)){
findChildren($tree, $children);
}
$folders->free_result();
$mysqli->close();
return $tree;
}
$tree 和 $children 在传递给findChildren() 之前的输出:
Children Array Before findChildren
Array
(
[0] => Array
(
[folderId] => 2
[folderName] => home
[parentId] => 1
)
[1] => Array
(
[folderId] => 3
[folderName] => account
[parentId] => 2
)
[2] => Array
(
[folderId] => 4
[folderName] => bill
[parentId] => 2
)
[3] => Array
(
[folderId] => 6
[folderName] => work
[parentId] => 2
)
)
Tree Array Before findChildren
Array
(
[0] => Array
(
[folderId] => 1
[folderName] => work
[parentId] =>
)
[1] => Array
(
[folderId] => 5
[folderName] => hello
[parentId] =>
)
)
【问题讨论】:
标签: php mysql arrays multidimensional-array directory-structure