我有having tree issues的历史记录,我在research上完成了很多topic。我想也许我在这个钉子上使用了错误的锤子。
我正在尝试从数组创建树层次结构数据集,但不仅仅是已定义父子关系的数据集。它将数据集中的一个数组与另一个数组进行比较,然后将相同的数据组合在一起,并将差异分支到一个新数组中。
这背后的原因是用于搜索预期较大的数据集(例如ACL),并通过API调用返回此格式。
来自PHP和MySQL查询,我返回一个看起来像这样的数组(键值对可以是任何东西,这只是一个概括):
$testArray = array(
array('group' => 'Group1','group_id' => '1','portal' => 'Portal1','portal_id' => '1','role' => 'Role1','role_id' => '1','permission' => 'Permission1','permission_id' => '1'),
array('group' => 'Group1','group_id' => '1','portal' => 'Portal1','portal_id' => '1','role' => 'Role1','role_id' => '1','permission' => 'Permission2','permission_id' => '2'),
array('group' => 'Group1','group_id' => '1','portal' => 'Portal2','portal_id' => '2','role' => 'Role1','role_id' => '1','permission' => 'Permission3','permission_id' => '3'),
array('group' => 'Group1','group_id' => '1','portal' => 'Portal2','portal_id' => '2','role' => 'Role2','role_id' => '2','permission' => 'Permission1','permission_id' => '1'),
array('group' => 'Group2','group_id' => '2','portal' => 'Portal1','portal_id' => '1','role' => 'Role3','role_id' => '3','permission' => 'Permission1','permission_id' => '1'),
array('group' => 'Group2','group_id' => '2','portal' => 'Portal1','portal_id' => '1','role' => 'Role3','role_id' => '3','permission' => 'Permission2','permission_id' => '2')
);
伪输出后:
(Group_Name = Group1, Group_ID = 1)
(Portal_Name = Portal1, Portal_id = 1) (Portal_Name = Portal2, Portal_id = 2)
(Role,RoleID) (Role,RoleID)
(Permission,Permission_id) (Permission,Permission_id)
此处有 WIP 功能。它遍布ATM的所有地方,但这是我的目标所在:
//List of keyList to compare against
if(is_array($testArray[0])) {
$keyList = array_keys($testArray[0]);
}
function buildTree(&$data, $keyList) {
$branch = array();
$tree = array();
foreach($keyList as $k => &$keyName) {
//Each row in the data set is an array
foreach($data as $rowKey => &$rowArraySet) {
//If the branch key exists, and matches
if(isset($rowArraySet[$keyName]) && isset($keyList[$keyName]) &&
isset($branch[$keyList[$keyName]][$rowArraySet[$keyName]])) {
if(isset($data[1+$rowKey]) &&
$rowArraySet[$keyName] == $data[1+$rowKey][$keyName]) {
$branch[$keyName][$rowArraySet[$keyName]] = array_diff($rowArraySet, $rowArraySet[$keyName]);
unset($data[$rowKey]);
}
}
//If the branch does not exist, and matches next array record
else if(isset($data[1+$rowKey]) &&
$rowArraySet[$keyName] == $data[1+$rowKey][$keyName]) {
$branch[$keyName] = array();
$branch[$keyName][$rowArraySet[$keyName]] = array_diff($rowArraySet, array($keyName => $rowArraySet[$keyName]));
unset($data[$rowKey]);
}
//Create new branch key (no match)
else if(isset($rowArraySet[$keyName])) {
$branch[$keyName] = array();
$branch[$keyName][$rowArraySet[$keyName]] = array_diff($rowArraySet, array($keyName => $rowArraySet[$keyName]));
unset($data[$rowKey]);
}
echo $rowKey;
}
//Remove keyName from checking
unset($keyList[$k]);
//Compare branch
//$tree[] = buildTree($branch, &$keyList);
//echo '<pre>';
//var_dump($branch);
//die;
}
return $tree;
}
是的,我知道程序员和未记录的代码在地狱中占有一席之地。我的想法是这样的:
与往常一样,任何指导都表示赞赏。我觉得我已经接近搞清楚了,我只是无法绕过它。谢谢。
按推荐编辑。