English 中文(简体)
将一系列的亲子关系转换成一棵树?
原标题:Convert a series of parent-child relationships into a tree?

我被困住了 我想我需要写一个循环方法...

我试图将一系列亲子关系转换成一棵等级树,

这是我可能有的输入数据的例子:

$input = array(
    array(
             itemGroupID  => 1,
             childItemGroupID  => 2
        ),
    array(
             itemGroupID  => 1,
             childItemGroupID  => 3
        ),
    array(
             itemGroupID  => 1,
             childItemGroupID  => 4
        ),
    array(
             itemGroupID  => 1,
             childItemGroupID  => 212
        ),
    array(
             itemGroupID  => 1,
             childItemGroupID  => 339
        ),
    array(
             itemGroupID  => 1,
             childItemGroupID  => 336
        ),
    array(
             itemGroupID  => 1,
             childItemGroupID  => 6
        ),
    array(
             itemGroupID  => 1,
             childItemGroupID  => 5
        ),
    array(
             itemGroupID  => 6,
             childItemGroupID  => 8
        ),
    array(
             itemGroupID  => 6,
             childItemGroupID  => 9
        ),
    array(
             itemGroupID  => 6,
             childItemGroupID  => 10
        ),
    array(
             itemGroupID  => 6,
             childItemGroupID  => 11
        ),
    array(
             itemGroupID  => 6,
             childItemGroupID  => 12
        ),
    array(
             itemGroupID  => 6,
             childItemGroupID  => 13
        ),
    array(
             itemGroupID  => 6,
             childItemGroupID  => 74
        ),
    array(
             itemGroupID  => 9,
             childItemGroupID  => 15
        ),
    array(
             itemGroupID  => 10,
             childItemGroupID  => 16
        ),
    array(
             itemGroupID  => 11,
             childItemGroupID  => 17
        ),
    array(
             itemGroupID  => 12,
             childItemGroupID  => 18
        ),
    array(
             itemGroupID  => 13,
             childItemGroupID  => 19
        ),
    array(
             itemGroupID  => 74,
             childItemGroupID  => 75
        )
 );

我希望以如下格式获取回数据:

$output = array(
    array(
         itemGroupID  => 1,
         children  => array(
              array(
                   itemGroupID  => 2                   
              ),
              array(
                   itemGroupID  => 3                   
              ),
              array(
                   itemGroupID  => 4                   
              ),
              array(
                   itemGroupID  => 212                   
              ),
              array(
                   itemGroupID  => 339                   
              ),
              array(
                   itemGroupID  => 336                  
              ),
              array(
                   itemGroupID  => 6,
                   children  => array(
                      array(
                           itemGroupID  => 8                  
                      ),
                      array(
                           itemGroupID  => 9,
                           children  => array(
                              array(
                                   itemGroupID  => 15
                              )   
                          )                 
                      ), 
                      array(
                           itemGroupID  => 10,
                           children  => array(
                              array(
                                   itemGroupID  => 16
                              )   
                          )                  
                      ), 
                      array(
                           itemGroupID  => 11,
                           children  => array(
                              array(
                                   itemGroupID  => 17
                              )   
                          )                   
                      ), 
                      array(
                           itemGroupID  => 12,
                           children  => array(
                              array(
                                   itemGroupID  => 18
                              )   
                          )                   
                      ), 
                      array(
                           itemGroupID  => 13,
                           children  => array(
                              array(
                                   itemGroupID  => 19
                              )   
                          )                   
                      ), 
                      array(
                           itemGroupID  => 74,
                           children  => array(
                              array(
                                   itemGroupID  => 75
                              )   
                          )                   
                      )                    
                  )               
              ),
              array(
                   itemGroupID  => 5                   
              )
         )
    )
);

(我担心如果你从第一个节点重现, 可能会发现这已经是... )

Btw, 元素I输出可以用来获得起始指数, 每个等级链的起始指数 。

private function _arraySearch($arr, $callback)
{
    foreach ($arr as $key => $item) {
        if ($callback($item)) {
            return $key;
        }
    }
    return false;
}

private function _findRootsOfItemGroupTree($activeItemGroupChildren)
{
    $searchArray = $activeItemGroupChildren;
    $roots = array();
    foreach ($activeItemGroupChildren as $itemGroupChild) {
        $parentItemGroupID = $itemGroupChild[ itemGroupID ];

        $found = array_filter($searchArray, function ($element) use ($parentItemGroupID) {
            return $element[ childItemGroupID ] == $parentItemGroupID;
        });

        $rootItemGroupID = $parentItemGroupID;
        if (count($found) == 0
            && $this->_arraySearch($roots,
                function ($element) use ($rootItemGroupID) {
                    return $element[ itemGroupID ] == $rootItemGroupID;
                }) === false) {

            $roots[] = $itemGroupChild;
        }
    }
    return $roots;
}

然而,我现在需要利用这些信息来创建一个新的关联阵列。 我无法找到方法。 (我将在几分钟后张贴一些工作成果。 ) 。 )

想法吗?

注:假设这一结构没有循环循环循环,即使从技术上说它们可能存在。

最佳回答

If I understand your problem correctly, this should work. Notice how I call the orderMe function inside the function to make it recursive.

function orderMe($input, $parentId)
{
    $return = array($parentId => array( itemGroupID  => $parentId));
    $childs = array();
    foreach ($input as $i)
    {
        if ($i[ itemGroupID ] == $parentId)
        {
            $return[$i[ itemGroupID ]][ children ][$i[ childItemGroupID ]] = array( itemGroupID  => $i[ childItemGroupID ]);
            $childs[] = $i[ childItemGroupID ];
        }

        if (in_array($i[ childItemGroupID ], $childs))
        {
            $allChilds = orderMe($input, $i[ childItemGroupID ]);
            if (!empty($allChilds[$i[ childItemGroupID ]][ children ]))
                $return[$i[ itemGroupID ]][ children ][$i[ childItemGroupID ]] =  $allChilds;
        }
    }

    return $return;
}

print_r(orderMe($input, 1));

产出:

array (
  1 => 
  array (
     itemGroupID  => 1,
     children  => 
    array (
      2 => 
      array (
         itemGroupID  => 2,
      ),
      3 => 
      array (
         itemGroupID  => 3,
      ),
      4 => 
      array (
         itemGroupID  => 4,
      ),
      212 => 
      array (
         itemGroupID  => 212,
      ),
      339 => 
      array (
         itemGroupID  => 339,
      ),
      336 => 
      array (
         itemGroupID  => 336,
      ),
      6 => 
      array (
        6 => 
        array (
           itemGroupID  => 6,
           children  => 
          array (
            8 => 
            array (
               itemGroupID  => 8,
            ),
            9 => 
            array (
              9 => 
              array (
                 itemGroupID  => 9,
                 children  => 
                array (
                  15 => 
                  array (
                     itemGroupID  => 15,
                  ),
                ),
              ),
            ),
            10 => 
            array (
              10 => 
              array (
                 itemGroupID  => 10,
                 children  => 
                array (
                  16 => 
                  array (
                     itemGroupID  => 16,
                  ),
                ),
              ),
            ),
            11 => 
            array (
              11 => 
              array (
                 itemGroupID  => 11,
                 children  => 
                array (
                  17 => 
                  array (
                     itemGroupID  => 17,
                  ),
                ),
              ),
            ),
            12 => 
            array (
              12 => 
              array (
                 itemGroupID  => 12,
                 children  => 
                array (
                  18 => 
                  array (
                     itemGroupID  => 18,
                  ),
                ),
              ),
            ),
            13 => 
            array (
              13 => 
              array (
                 itemGroupID  => 13,
                 children  => 
                array (
                  19 => 
                  array (
                     itemGroupID  => 19,
                  ),
                ),
              ),
            ),
            74 => 
            array (
              74 => 
              array (
                 itemGroupID  => 74,
                 children  => 
                array (
                  75 => 
                  array (
                     itemGroupID  => 75,
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
      5 => 
      array (
         itemGroupID  => 5,
      ),
    ),
  ),
)
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签