PHP:重新排序数组以显示层次结构

时间:2011-09-20 20:59:14

标签: php

如何将第一个阵列转换为第二个阵列?目标是创建一个显示层次结构的数组,基于location_id和parent_id。每个location_name应该在一个数组中,其键是其parent_id。

忽略我给location_name的值。没有值为parent_id == NULL,这些是顶级项目。

第一个阵列:

    Array
    (
        [0] => stdClass Object
            (
                [location_id] => 1
                [location_name] => Town 1
                [parent_id] => 
            )

        [1] => stdClass Object
            (
                [location_id] => 2
                [location_name] => Town 1.1
                [parent_id] => 
            )

        [2] => stdClass Object
            (
                [location_id] => 3
                [location_name] => Town 1.2
                [parent_id] => 1
            )

        [3] => stdClass Object
            (
                [location_id] => 4
                [location_name] => Town 1.3
                [parent_id] => 1
            )

        [4] => stdClass Object
            (
                [location_id] => 5
                [location_name] => town 1.1.1
                [parent_id] => 2
            )

        [5] => stdClass Object
            (
                [location_id] => 6
                [location_name] => Town 1.1.2
                [parent_id] => 3
            )
);

结果数组应为:

Array(
        'Town 1' = array(
            'town 1.2',
            'town 1.3' = array(
                 'town 1.1.2'
             )
        ),
        'Town 2' = array(
             'town 1.1.1'
        )
    );

编辑:基于Rijk答案的工作解决方案

function _order_locs($parent, $array)
    {
        $return = array();
        foreach ( $array as $town )
        {
            if ( $town->parent_id == $parent )
            {
                $set = $this->_order_locs( $town->location_id, $array );               
                if( $this->_menu_is_parent($town->location_id, $array) ) $return[$town->location_name] = $set;
                else $return[] = $town->location_name;            
            }
        }

        return $return;
    }

    function _menu_is_parent($id, $array)
    {
        foreach( $array as $a )
        {
            if( $a->parent_id == $id ) return TRUE;
        }
    }

2 个答案:

答案 0 :(得分:3)

你必须使用递归函数(一个自己调用的函数)遍历它:

function getChilds( $parent, $array ) {
    $return = array();
    foreach ( $array as $town ) {
        if ( $town['location_id'] == $parent ) {
            $return[] = array(
                'name' => $town['location_name'],
                'childs' => getChilds( $town['location_id'], $array )
            );
        }
    }
    return $return;
}

$towns_tree = getChilds( 0, $towns );

可能不会马上工作,但是这会给你一个很好的机会来玩代码并熟悉这个概念;)

答案 1 :(得分:0)

以下是一些或多或少可以满足您需求的代码。你必须根据自己的喜好调整它。

<?php

Class Node {
   public $id;
   public $parent_id;
   public $value;
   public $children;
   public $depth;

   function __construct($id, $parent_id, $value) {
      $this->id = $id;
      $this->parent_id = $parent_id;
      $this->value = $value;
          $this->children = array();
      $this->depth = 0;
   }

   function add_child(Node $new_child) {
      if ($new_child->parent_id == $this->id) {
         $this->children[$new_child->id] = $new_child;
         $this->children[$new_child->id]->depth = $this->depth + 1;
      } else {
         foreach ($this->children as $child) {
            $child->add_child($new_child);
         }
      }
   }

   function to_array() {
      if (count($this->children) > 0) {
         $arr = array();
         foreach ($this->children as $child) {
            array_push($arr, $child->to_array());
         }
         return array($this->value => $arr);
      } else {
         return $this->value;
      }
   }

   function str() {
      echo str_repeat(" ", $this->depth) . $this->value . "\n";
      foreach ($this->children as $child) {
         $child->str();
      }
   }
}

?>

以下是一些用于测试它的示例代码:

<?php

$arr = Array(
   array('location_id' => 1,
         'location_name' => 'Town 1',
         'parent_id' => 0),

   array('location_id' => 2,
         'location_name' => 'Town 1.1',
         'parent_id' => 0),

   array('location_id' => 3,
         'location_name' => 'Town 1.2',
         'parent_id' => 1),

   array('location_id' => 4,
         'location_name' => 'Town 1.3',
         'parent_id' => 1),

   array('location_id' => 5,
         'location_name' => 'Town 1.1.1',
         'parent_id' => 2),

       array('location_id' => 6,
         'location_name' => 'Town 1.1.2',
         'parent_id' => 3)
);

$root = new Node(0, 0, 'root');
foreach ($arr as $item) {
   $node = new Node($item['location_id'],
                    $item['parent_id'],
                    $item['location_name']);
   $root->add_child($node);
}
$tree = $root->to_array();
$tree = $tree['root'];
var_dump($tree);

?>