[英]php recursive get parents
I am trying to get jquery.treeview.js to fold out to the just created folder. 我试图让jquery.treeview.js折叠到刚刚创建的文件夹。 Think I need an array of "parents" to set class to "open".
认为我需要一个“父母”数组来将类设置为“开放”。
Other suggestions works with me ;-) (find this a bit too much but cannot find another way to do it. 其他建议对我有用;-)(发现的太多了,但是找不到其他方法来解决。
Having an array like this: 具有这样的数组:
array(7) {
[126]=>
array(4) {
["folder_id"]=>
string(3) "126"
["folder_name"]=>
string(3) "555"
["folder_parent"]=>
string(3) "125"
}
[2]=>
array(4) {
["folder_id"]=>
string(1) "2"
["folder_name"]=>
string(14) "Administration"
["folder_parent"]=>
string(1) "1"
}
[7]=>
array(4) {
["folder_id"]=>
string(1) "7"
["folder_name"]=>
string(5) "Britt"
["folder_parent"]=>
string(1) "2"
}
[4]=>
array(4) {
["folder_id"]=>
string(1) "4"
["folder_name"]=>
string(9) "Documents"
["folder_parent"]=>
string(1) "3"
}
[3]=>
array(4) {
["folder_id"]=>
string(1) "3"
["folder_name"]=>
string(14) "Infrastructure"
["folder_parent"]=>
string(1) "1"
}
[1]=>
array(4) {
["folder_id"]=>
string(1) "1"
["folder_name"]=>
string(4) "root"
["folder_parent"]=>
string(1) "0"
}
[125]=>
array(4) {
["folder_id"]=>
string(3) "125"
["folder_name"]=>
string(13) "test-deleteme"
["folder_parent"]=>
string(1) "7"
}
}
I would like to get the parents from selected folder_id. 我想从选定的folder_id中获取父母。
getting data for folder_id=126 should return an array with parents {1,2,7,122} 获取folder_id = 126的数据应返回带有父项{1,2,7,122}的数组
Anyone? 任何人?
Well, here is mine with recursive: 好吧,这是我的递归代码:
function getParent($folder_id, $data, $parents=array()) {
$parent_id = isset($data[$folder_id]) ? $data[$folder_id]['folder_parent'] : 0;
if ($parent_id > 0) {
array_unshift($parents, $parent_id);
return getParent($parent_id, $data, $parents);
}
return $parents;
}
//Usage
print_r(getParent(126, $your_folders));
It seems like I had plagiarized mancuernita's solution I hereby apologize. 特此道歉,我似乎抄袭了mancuernita的解决方案 。 It's just similar, but I'm not copying!
只是相似,但我没有复制!
You could do something like: 您可以执行以下操作:
function yourFunction(id, array) {
sol = array();
while (id) {
id = find_parent(id, array);
array_push(sol, id);
}
return sol;
}
function find_parent(id, array) {
return array[id]["folder_parent"];
}
I haven't tried the code, and probably it needs some work more. 我没有尝试过代码,可能还需要更多工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.