This article illustrates the tree-structured data access class implemented by PHP. Share to everyone for your reference.
The specific implementation code is as follows:
Copy Code code as follows:
<?php
/**
* tanphp Framework
*
*
* @category tanphp
* @package Data_structure
* @version $Id: tree.php 25024 2012-11-26 22:22:22 Tanbo $
*/
/**
* Tree structure data access class
*
* For fast access to tree-structured data
*
* @param array $arr parameter must be a standard two-dimensional array containing the indexed field (ID) and the field (path) that represents the tree structure, as shown in example
*
* @example <code>
* $arr = Array (
* Array (' ID ' => 1, ' name ' => ' php ', ' path ' => ' 1 '),
* Array (' ID ' => 3, ' name ' => ' php1 ', ' path ' => ' 1-3 '),
* Array (' ID ' => 2, ' name ' => ' mysql ', ' path ' => ' 2 '),
* Array (' ID ' => 6, ' name ' => ' mysql1 ', ' path ' => ' 2-6 '),
* Array (' ID ' => 7, ' name ' => ' mysql2 ', ' path ' => ' 2-7 '),
* Array (' ID ' => 5, ' name ' => ' php11 ', ' path ' => ' 1-3-5 '),
* Array (' ID ' => 4, ' name ' => ' php2 ', ' path ' => ' 1-4 '),
* );
* $cate = new tree ($arr);
*
* $data = $cate->getchild (2);
*
* Print_r ($data->toarray ());
* </code>
*
*/
Class Tree
{
Public $_info; Node information
Public $_child = Array (); Child nodes
Private $_parent; Parent node
Private $_data; Temporary data for the current operation
private static $_indexs = Array (); Index of all nodes
private static $_index_key = ' id '; Index keys
private static $_tree_key = ' path '; Tree structure Expression key
private static $_tree_delimiter = '-'; Attribute Structure Expression Separator
/**
* Constructor
*
* @param array $arr
* @param boole $force _sort If True, the $arr is forced to be sorted
* @return void
*/
Public function __construct (array $arr = Array (), $force _sort=true)
{
if ($force _sort = = True) {
$arr = $this->_array_sort ($arr, Self::$_tree_key);
}
if (!emptyempty ($arr)) {
$this->_init ($arr);
}
}
/**
* Initial storage of tree-shaped data
*
* @param array $arr
* @return void
*/
Private function _init (array $arr)
{
foreach ($arr as $item) {
$path = $item [Self::$_tree_key];
$paths = Explode (Self::$_tree_delimiter, $path);
$count _paths = count ($paths);
$parent _id = isset ($paths [$count _paths-2])? $paths [$count _paths-2]: NULL;
if ($count _paths>1//If there is a parent
&& array_key_exists ($parent _id, Self::$_indexs)//parent has been indexed
&& self::$_indexs[$parent _id] instanceof Tree//Parent is tree object
) {
self::$_indexs[$parent _id]->addchild ($item);
} elseif ($count _paths = = 1) {
$this->addchild ($item);
} else {
throw new Exception ("Path data Error". Var_export ($item, true));
}
}
Print_r (SELF::$_INDEXS);
}
/**
* Add child nodes
*
* @param array $item
* @return void
*/
Public function AddChild (array $item, $parent = NULL)
{
$child = new Tree ();
$child->_info = $item;
$child->_parent = $parent = = NULL? $this: $parent;
$child->_parent->_child[] = $child;
$this->_addindex ($item, $child->_getself ());
}
/**
* Add nodes to index
*
* @param array $item
* @param mix $value
* @return void
*/
Private function _addindex (array $item, $value)
{
if (Array_key_exists (Self::$_index_key, $item) && is_int ($item [Self::$_index_key])) {
self::$_indexs[$item [Self::$_index_key]] = $value;
} else {
throw new Exception ("ID field does not exist or is not a string");
}
}
/**
* Get a reference to yourself
*
* @return Tree Object Quote
*/
Private Function _getself ()
{
return $this;
}
/**
* Gets the child nodes of the node with the specified ID
*
* @param int $id
* @return Tree Object
*/
Public Function Getchild ($id)
{
$data = self::$_indexs[$id]->_child;
$this->_data = $data;
return $this;
}
/**
* Gets the parent node of the node with the specified ID
*
* @param int $id
* @return Tree Object
*/
Public Function GetParent ($id)
{
$data = self::$_indexs[$id]->_parent;
$this->_data = $data;
return $this;
}
/**
* Gets the sibling node of the node with the specified ID
*
* @param int $id
* @return Tree Object
*/
Public Function Getbrother ($id)
{
$data = self::$_indexs[$id]->_parent->_child;
$this->_data = $data;
return $this;
}
/**
* Convert a Tree object to an array
*
* @param object $object
* @return Array
*/
Public Function ToArray ($obj = NULL)
{
$obj = ($obj = = NULL)? $this->_data: $obj;
$arr = Array ();
$_arr = Is_object ($obj)? $this->_getbaseinfo ($obj): $obj;
if (Is_array ($_arr)) {
foreach ($_arr as $key => $val) {
$val = (Is_array ($val) | | | is_object ($val))? $this->toarray ($val): $val;
$arr [$key] = $val;
}
} else {
throw new Exception ("_arr is not an array");
}
return $arr;
}
/**
* Filter _parent fields so as not to cause infinite cycle
*
* @param object $obj
* @return void
*/
Private Function _getbaseinfo ($obj)
{
$vars = Get_object_vars ($obj);
$baseInfo [' _info '] = $vars [' _info '];
$baseInfo [' _child '] = $vars [' _child '];
return $baseInfo;
}
/**
* Two-dimensional array ordering
*
* Sort the two-dimensional array in ascending or descending order according to the specified key name
*
* @param array $arr two-dimensional arrays
* @param string $keys
* @param string $type must be ASC or DESC
* @throws throw an exception when the argument is illegal
* @return returns the sorted array
*/
Private function _array_sort (array $arr, $keys, $type = ' asc ') {
if (!is_string ($keys)) {
throw new Exception ("Illegal parameter keys: The type of the parameter keys must be a string");
}
$keysvalue = $new _array = Array ();
foreach ($arr as $k => $v) {
if (!is_array ($v) | |!isset ($v [$keys])) {
throw new Exception ("parameter arr is not a two-dimensional array or arr child element does not exist the key ' {$keys} '");
}
$keysvalue [$k] = $v [$keys];
}
Switch ($type) {
Case ' ASC ':
Asort ($keysvalue);
Break
Case ' desc ':
Arsort ($keysvalue);
Break
Default
throw new Exception ("Illegal parameter type: The value of the parameter type must be ' ASC ' or ' desc '");
}
Reset ($keysvalue);
foreach ($keysvalue as $k => $v) {
$new _array[$k] = $arr [$k];
}
return $new _array;
}
}
?>
I hope this article will help you with your PHP program design.