从动态数据集生成PHP数组树结构

时间:2014-04-04 17:06:19

标签: php recursion hierarchical

我有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;
}

是的,我知道程序员和未记录的代码在地狱中占有一席之地。我的想法是这样的:

  1. 从集合中的第一个数组中捕获密钥(这是父子关系,至少是要检查的顺序)
  2. 使用键将每个阵列与集合中的每个阵列进行比较(如果存在/设置等)
  3. 匹配值创建一个新分支并移动数组,将它们组合起来
  4. 检查下一组,并检查分支中是否存在密钥
  5. 完成后对分支进行递归排序
  6. 将分支返回到树阵列和输出树
  7. 与往常一样,任何指导都表示赞赏。我觉得我已经接近搞清楚了,我只是无法绕过它。谢谢。

    按推荐编辑。

0 个答案:

没有答案