不确定您是否已经找到答案,但我今天也在寻找同样的解决方案,最终我自己创建了一个解决方案。下面的代码是我刚刚创建的类,它适用于 PHP 数组和对象,并且可以递归到无限数量的维度。
GitHub
https://github.com/DukeOfMarshall/PHP-Array-Heirarchy-Display
使用此代码的简单示例:
<?php
require_once('Arrays.class.php');
$display = new Array_Functions();
$display->display_hierarchy($multidimensional_array);
?>
还有其他选项可以设置,但那只是一个数组或对象的直接层次显示。
<?php
class Array_Functions {
public $number_of_tabs = 3;
private $counter = 0;
public $display_square_brackets = TRUE;
public $display_squiggly_brackets = FALSE;
public $display_parenthesis = FALSE;
public $display_apostrophe = TRUE;
public $display_quotes = FALSE;
public function __construct(){
}
public function display_hierarchy($array, $key_bookends = '', $key_padding = '', $number_of_tabs_to_use = ''){
$array = $this->convert_object_to_array($array);
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();
}
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;
}
}
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;
}
}
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)." => {<BR>";
$this->counter++;
$this->display_hierarchy($value);
$this->counter--;
$this->insert_tabs();
echo '} <BR>';
}else{
echo $this->display_padding($key)." => ".$value.'<BR>';
}
}
}
private function insert_tabs(){
for($i=1; $i<=$this->counter; $i++){
for($x=1; $x<=$this->number_of_tabs; $x++){
echo ' ';
}
}
}
public function convert_object_to_array($object){
$object = json_decode(json_encode($object), TRUE);
return $object;
}
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;
}
}
?>