Home > php教程 > PHP源码 > 无限型将排序的一维数组压制成

无限型将排序的一维数组压制成

PHP中文网
Release: 2016-05-23 13:07:53
Original
1196 people have browsed it

将一维数组压制成为树状结构,例如
1,11,111,1111
1,11,111,1112
1,11,112,1121
压制成为
1
_11
____111
_______1111
_______1112
____112
_______1121
以数组形式存储,最后一级(叶子节点)以一维数组存储,其余各级均以[id=>[],data=>[]]
的格式存储

<?php


class array_2_tree {
//更改方案,使用直接暴力压制下去的方式
private $tree=[];
private $row=[];

private $id_names=false;
private $data_names=false;

private $row_keys=0;
private $row_size=0;

function __construct($opt){
$this->init($opt);
}

private function get_row_value($pos){
if(!$this->row_keys){
$this->row_keys=array_keys($this->row);
}
return $this->row[$this->row_keys[$pos]];
}

function init($opt){
if($opt[&#39;id_names&#39;] && is_array($opt[&#39;id_names&#39;])){
$this->id_names=$opt[&#39;id_names&#39;];
}
if($opt[&#39;data_names&#39;] && is_array($opt[&#39;data_names&#39;])){
$this->data_names=$opt[&#39;data_names&#39;];
}
}
function pick(&$row){
$this->row=$row;
$this->row_size=sizeof($row);
$this->smash($this->tree);
}
function get_tree(){
return $this->tree;
}
private function smash(&$node,$key_index=0){
$id_name=$this->get_id_name($key_index);
$data_name=$this->get_data_name($key_index);
if(sizeof($this->row)<$key_index){
return;
}
if(!sizeof($node)){
if((sizeof($this->row)-1)==($key_index)){
$node[]=$this->get_row_value($key_index);
return;
}else{
$node[]=[
$id_name=>$this->get_row_value($key_index),
$data_name=>[]
];
}
$new_node=&$node[0][$data_name];
$node=&$new_node;
}else{
$size=sizeof($node);
if( ($this->row_size-1) == $key_index){
$node[]=$this->get_row_value($key_index);
return;
}
if( ($node[$size-1][$id_name]) == ($this->get_row_value($key_index)) ){
$next_node=&$node[$size-1][$data_name];
$node=&$next_node;
}else{
$node[]=[
$id_name=>$this->get_row_value($key_index),
$data_name=>[]
];
$size=sizeof($node);
$next_node=&$node[$size-1][$data_name];
$node=&$next_node;
}
}
$this->smash($node,++$key_index);
}
private function get_id_name($k){
if($this->id_names && $this->id_names[$k]){
return $this->id_names[$k];
}else{
return &#39;id&#39;;
}
}
private function get_data_name($k){
if($this->data_names && $this->data_names[$k]){
return $this->data_names[$k];
}else{
return &#39;data&#39;;
}
}
}

?>
Copy after login
$a=[
[&#39;depid&#39;=>1,&#39;uid&#39;=>11,&#39;ymd&#39;=>1,&#39;v&#39;=>1],
[&#39;depid&#39;=>1,&#39;uid&#39;=>11,&#39;ymd&#39;=>2,&#39;v&#39;=>2],
[&#39;depid&#39;=>1,&#39;uid&#39;=>11,&#39;ymd&#39;=>3,&#39;v&#39;=>3],
[&#39;depid&#39;=>1,&#39;uid&#39;=>11,&#39;ymd&#39;=>4,&#39;v&#39;=>4],
[&#39;depid&#39;=>1,&#39;uid&#39;=>12,&#39;ymd&#39;=>2,&#39;v&#39;=>5],
[&#39;depid&#39;=>1,&#39;uid&#39;=>12,&#39;ymd&#39;=>3,&#39;v&#39;=>6],
[&#39;depid&#39;=>1,&#39;uid&#39;=>12,&#39;ymd&#39;=>4,&#39;v&#39;=>7],
[&#39;depid&#39;=>1,&#39;uid&#39;=>12,&#39;ymd&#39;=>5,&#39;v&#39;=>8],
[&#39;depid&#39;=>2,&#39;uid&#39;=>2,&#39;ymd&#39;=>1,&#39;v&#39;=>9],
[&#39;depid&#39;=>2,&#39;uid&#39;=>2,&#39;ymd&#39;=>2,&#39;v&#39;=>10],
[&#39;depid&#39;=>2,&#39;uid&#39;=>2,&#39;ymd&#39;=>3,&#39;v&#39;=>11],
[&#39;depid&#39;=>2,&#39;uid&#39;=>2,&#39;ymd&#39;=>3,&#39;v&#39;=>12]
];
$r=[];
$t=new array_2_tree();
foreach($a as $k=>$v){
$t->pick($v);
}
$r=$t->get_tree();
debug($r);
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template