Flat PHP Array to Hierarchy Tree

后端 未结 3 1478
天涯浪人
天涯浪人 2020-12-03 02:11

I have an array with the following keys

id   
parent_id
name

A sample array:

array(7) {
  [0]=>
  array(3) {
    [\"id\"         


        
3条回答
  •  无人及你
    2020-12-03 02:43

    Not sure if you've found an answer yet, but I was looking for the same solution today and finally ended up making my own solution. The below code is the class I just created and it works with PHP arrays and objects and is recursive to an unlimited number of dimensions.

    GitHub https://github.com/DukeOfMarshall/PHP-Array-Heirarchy-Display

    A simple example of using this code would be:

    display_hierarchy($multidimensional_array);
    
    ?>
    

    There are other options that can be set as well, but that was just a straight up heirarchal display of an array or object.

    convert_object_to_array($array);
    
        # If the $array variable is still not an array, then error out.
        # We're not going to fool around with your stupidity
        if(gettype($array) != 'array'){
            echo 'Value submitted was '.strtoupper(gettype($array)).' instead of ARRAY or OBJECT. Only arrays or OBJECTS are allowed Terminating execution.';
            exit();
        }
    
        # Establish the bookend variables
        if($key_bookends != '' || !$key_bookends){
            if(strtolower($key_bookends) == 'square'){
                $this->display_square_brackets      = TRUE;
                $this->display_squiggly_brackets    = FALSE;
                $this->display_parenthesis            = FALSE;
            }elseif(strtolower($key_bookends) == 'squiggly'){
                $this->display_square_brackets      = TRUE;
                $this->display_squiggly_brackets    = TRUE;
                $this->display_parenthesis            = FALSE;
            }elseif(strtolower($key_bookends) == 'parenthesis'){
                $this->display_square_brackets      = FALSE;
                $this->display_squiggly_brackets    = FALSE;
                $this->display_parenthesis          = TRUE;
            }elseif(!$key_bookends){
                $this->display_square_brackets      = FALSE;
                $this->display_squiggly_brackets    = FALSE;
                $this->display_parenthesis            = FALSE;
            }
        }
    
    
        # Establish the padding variables
        if($key_padding != '' || !$key_padding){
            if(strtolower($key_padding) == 'apostrophe'){
                $this->display_apostrophe = TRUE;
                $this->display_quotes       = FALSE;
            }elseif(strtolower($key_padding) == 'quotes'){
                $this->display_apostrophe = FALSE;
                $this->display_quotes       = TRUE;
            }elseif(!$key_padding){
                $this->display_apostrophe = FALSE;
                $this->display_quotes       = FALSE;
            }
        }       
    
        # Establish variable for the number of tabs
        if(isset($number_of_tabs_to_use) && $number_of_tabs_to_use != ''){
            $this->number_of_tabs = $number_of_tabs_to_use;
        }
    
        foreach($array as $key => $value){
            $this->insert_tabs();
    
            if(is_array($value)){
                echo $this->display_padding($key)." => {
    "; $this->counter++; $this->display_hierarchy($value); $this->counter--; $this->insert_tabs(); echo '}
    '; }else{ echo $this->display_padding($key)." => ".$value.'
    '; } } } # Inserts tab spaces for sub arrays when a sub array triggers a branch in the heirarchy # Helps to make the display more human readable and easier to understand private function insert_tabs(){ for($i=1; $i<=$this->counter; $i++){ for($x=1; $x<=$this->number_of_tabs; $x++){ echo ' '; } } } # Takes a PHP object and converts it to an array # Works with single dimension and multidimensional arrays public function convert_object_to_array($object){ $object = json_decode(json_encode($object), TRUE); return $object; } # Sets the displayed padding around the array keys when printed on the screen public function display_padding($value){ $default_container = "['VALUE_TO_REPLACE']"; $value = str_replace("VALUE_TO_REPLACE", $value, $default_container); if($this->display_square_brackets){ }elseif($this->display_squiggly_brackets){ $value = str_replace('[', '{', $value); $value = str_replace(']', '}', $value); }elseif($this->display_parenthesis){ $value = str_replace('[', '(', $value); $value = str_replace(']', ')', $value); }else{ $value = str_replace('[', '', $value); $value = str_replace(']', '', $value); } if($this->display_apostrophe){ }elseif($this->display_quotes){ $value = str_replace("'", '"', $value); }else{ $value = str_replace("'", '', $value); } return $value; } } ?>

提交回复
热议问题