将多维数组转换为嵌套的set数组

时间:2012-02-22 22:07:33

标签: php arrays nested-sets

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

    $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'
         )
    }

感谢任何帮助!

3 个答案:

答案 0 :(得分:1)

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

$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);

答案 1 :(得分:1)

对于来这里寻找解决方案的任何人来说,loneTraceur's 解决方案都可以正常工作。但是,我在 OOP 中需要一个,所以这是我的版本。

#include<iostream>
#include<string>

using namespace std;

int main()
{
    int count=0;
    string str;

    cout<<"Enter the characters for your string:";
    getline(cin, str);

    for (char c: str) {
        if (c == 'a' || c == 'A')
            count++;
    }
    cout<<"Number of 'a' and 'A' in your string is "<<count<<"." << endl;
    return 0;
}

你会像这样运行它:

<?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'];
    }
}

此代码基于 the other answer

答案 2 :(得分:0)

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);