将多维数组转换为嵌套集合数组

发布于 2025-01-08 08:45:32 字数 655 浏览 1 评论 0原文

我有一个小问题需要帮助。 我试图将多维数组转换为具有左右嵌套设置值的扁平数组,如下所示:

    $array = {
         'id' => 1
         'name' => 'john'
         'childs' => array(
             array(
                 'id' => 1
                 'name' => 'jane'
             )
          )        
    }

感谢

    $array = {
         array(
           'id' => 1,
           'name' => 'john'
           'left' => '1'
           'right' => '4'
         ),
         array(
           'id' => 1,
           'name' => 'jane'
           'left' => '2'
           'right' => '3'
         )
    }

任何帮助!

im having a little problem i need some help with.
Im trying to convert a multidimensional array into a flatten array with nested set values right and left like so:

    $array = {
         'id' => 1
         'name' => 'john'
         'childs' => array(
             array(
                 'id' => 1
                 'name' => 'jane'
             )
          )        
    }

to

    $array = {
         array(
           'id' => 1,
           'name' => 'john'
           'left' => '1'
           'right' => '4'
         ),
         array(
           'id' => 1,
           'name' => 'jane'
           'left' => '2'
           'right' => '3'
         )
    }

Any help is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫烠⑼条掵仅有一顆心 2025-01-15 08:45:32

我问了一个非常相似的问题并得到了结果,所以我想我会把它发送给你。我意识到这是一个相当古老的话题,但仍然值得得到答案。我已经包含了我的数据,但很容易适合您的数据。

$JSON = '[{"id":1,"children":[{"id":2,"children":[{"id":3},{"id":4}]},{"id":5}]}]';
$cleanJSON = json_decode($JSON,true);

$a_newTree = array();       

function recurseTree($structure,$previousLeft) 
{
    global $a_newTree;  // Get global Variable to store results in.

    $indexed = array();                     // Bucket of results.       
    $indexed['id'] = $structure['id'];      // Set ID
    $indexed['left'] = $previousLeft + 1;   // Set Left

    $lastRight = $indexed['left'];

    $i_count = 0;
    if ($structure['children'])
    {
        foreach ($structure['children'] as $a_child)
        {
            $lastRight = recurseTree($structure['children'][$i_count],$lastRight);
            $i_count++;
        }
    }

    $indexed['right'] = $lastRight + 1;     // Set Right

    array_push($a_newTree,$indexed);        // Push onto stack

    return $indexed['right'];       
}

recurseTree($cleanJSON[0],0);
print_r($a_newTree);

I asked a very similar question and got a result, so thought I'd send it on for you. I realise this is quite an old topic, but still worth getting an answer. I've included my data, but would be easily adapted for yours.

$JSON = '[{"id":1,"children":[{"id":2,"children":[{"id":3},{"id":4}]},{"id":5}]}]';
$cleanJSON = json_decode($JSON,true);

$a_newTree = array();       

function recurseTree($structure,$previousLeft) 
{
    global $a_newTree;  // Get global Variable to store results in.

    $indexed = array();                     // Bucket of results.       
    $indexed['id'] = $structure['id'];      // Set ID
    $indexed['left'] = $previousLeft + 1;   // Set Left

    $lastRight = $indexed['left'];

    $i_count = 0;
    if ($structure['children'])
    {
        foreach ($structure['children'] as $a_child)
        {
            $lastRight = recurseTree($structure['children'][$i_count],$lastRight);
            $i_count++;
        }
    }

    $indexed['right'] = $lastRight + 1;     // Set Right

    array_push($a_newTree,$indexed);        // Push onto stack

    return $indexed['right'];       
}

recurseTree($cleanJSON[0],0);
print_r($a_newTree);
我要还你自由 2025-01-15 08:45:32
function restructRecursive($array, $left = 1) {
  if (isset($array['childs'])) {
    $result = array();
    foreach ($array['childs'] as $child) {
      $result = array_merge($result, restructRecursive($child, $left+1));
    }
    unset($array['childs']);
  }
  $array['left'] = $left;
  return array_merge(array($array), $result);
}

$newStruct = restructRecursive($oldStruct);
function restructRecursive($array, $left = 1) {
  if (isset($array['childs'])) {
    $result = array();
    foreach ($array['childs'] as $child) {
      $result = array_merge($result, restructRecursive($child, $left+1));
    }
    unset($array['childs']);
  }
  $array['left'] = $left;
  return array_merge(array($array), $result);
}

$newStruct = restructRecursive($oldStruct);
囍孤女 2025-01-15 08:45:32

对于来到这里寻找解决方案的人来说,loneTraceur 解决方案效果很好。然而,我需要 OOP 中的一个,所以这是我的版本。

<?php

class NestedSet
{
    protected $tree = [];

    public function deconstruct($tree, $left = 0)
    {
        $this->flattenTree($tree, $left);
        return $this->tree;
    }

    protected function flattenTree($tree, $left)
    {
        $indexed = [];
        $indexed['id'] = $tree['id'];
        $indexed['_lft'] = $left + 1;

        $right = $indexed['_lft'];

        if (isset($tree['children']) && count($tree['children'])) {
            foreach ($tree['children'] as $child) {
                $right = $this->flattenTree($child, $right);
            }
        }

        $indexed['_rgt'] = $right + 1;

        $this->tree[] = $indexed;

        return $indexed['_rgt'];
    }
}

您可以这样运行它:

$NestedSet = new NestedSet;
$flat = $NestedSet->deconstruct($tree):

此代码基于另一个答案

For anyone coming here and looking for a solution, loneTraceur's solution works fine. However, I needed one in OOP, so here is my version of it.

<?php

class NestedSet
{
    protected $tree = [];

    public function deconstruct($tree, $left = 0)
    {
        $this->flattenTree($tree, $left);
        return $this->tree;
    }

    protected function flattenTree($tree, $left)
    {
        $indexed = [];
        $indexed['id'] = $tree['id'];
        $indexed['_lft'] = $left + 1;

        $right = $indexed['_lft'];

        if (isset($tree['children']) && count($tree['children'])) {
            foreach ($tree['children'] as $child) {
                $right = $this->flattenTree($child, $right);
            }
        }

        $indexed['_rgt'] = $right + 1;

        $this->tree[] = $indexed;

        return $indexed['_rgt'];
    }
}

You would run it like this:

$NestedSet = new NestedSet;
$flat = $NestedSet->deconstruct($tree):

This code is based on the other answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文