Unlimited suppression of sorted one-dimensional arrays into-PHP source code

Source: Internet
Author: User
Infinitely, sort one-dimensional arrays into a tree structure, for example



Suppress
1
_ 11
____ 111
_______ 1111
_______ 1112
____ 112
_______ 1121
It is stored as an array, and the last level (leaf node) is stored as a one-dimensional array. other levels are stored as [id => [], data => []
Format storage

 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 ['id _ names '] & is_array ($ opt ['id _ names']) {$ this-> id_names = $ opt ['id _ names '];} if ($ opt ['data _ names'] & is_array ($ opt ['data _ names']) {$ this-> data_names = $ opt ['data _ names '];} function pick (& $ row) {$ this-> row = $ row; $ this-> row_size = sizeof ($ row); $ this-> smash ($ this-> tree);} function g Et_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 ($ ke Y_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 'id';} private function get _ Data_name ($ k) {if ($ this-> data_names & $ this-> data_names [$ k]) {return $ this-> data_names [$ k];} else {return 'data' ;}}}?>
$a=[['depid'=>1,'uid'=>11,'ymd'=>1,'v'=>1],['depid'=>1,'uid'=>11,'ymd'=>2,'v'=>2],['depid'=>1,'uid'=>11,'ymd'=>3,'v'=>3],['depid'=>1,'uid'=>11,'ymd'=>4,'v'=>4],['depid'=>1,'uid'=>12,'ymd'=>2,'v'=>5],['depid'=>1,'uid'=>12,'ymd'=>3,'v'=>6],['depid'=>1,'uid'=>12,'ymd'=>4,'v'=>7],['depid'=>1,'uid'=>12,'ymd'=>5,'v'=>8],['depid'=>2,'uid'=>2,'ymd'=>1,'v'=>9],['depid'=>2,'uid'=>2,'ymd'=>2,'v'=>10],['depid'=>2,'uid'=>2,'ymd'=>3,'v'=>11],['depid'=>2,'uid'=>2,'ymd'=>3,'v'=>12]];$r=[];$t=new array_2_tree();foreach($a as $k=>$v){$t->pick($v);}$r=$t->get_tree();debug($r);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.