我创建了一个名为 HTMLElement 的自定义类。
class HTMLElement
{
private $name;
private $value;
function __construct($name,$value) {
$this->name = $name;
$this->value = $value;
}
public function getName(){
return $this->name;
}
public function setName($name){
$this->value = $name;
}
public function getValue(){
return $this->value;
}
public function setValue($value){
$this->value = $value;
}
public static function ConvertToHTMLElement(HTMLElement $obj){
return new HTMLElement($obj->name,$obj->value);
}
}
现在我想创建一个关联数组,使用属性 $ name 作为键, $ value 作为值。
$ value 可能包含字符串或数组或HTMLElement对象类型
这是我将我的价值传递给我的算法的地方。
$tree = new HTMLTree();
$newArr = $tree->ConvertToArray(new HTMLElement(
"table", (array(
new HTMLElement("tr", array(
new HTMLElement("td", "Name"),
new HTMLElement("td", "Age"),
new HTMLElement("td", "City")
)
)
))));
这是我的算法
class HTMLTree
{
public function ConvertToArray($htmlElement){
if(is_array($htmlElement)){
$arr =array();
foreach ($htmlElement as $singleElement){
$arr = $this->Convert($arr,$singleElement);
}
return $arr;
}else{
return $this->Convert(array(),$htmlElement);
}
}
private function Convert($arr,HTMLElement $element){;
$name = $element->getName();
$value = $element->getValue();
if(is_string($value)){
$arr =array(array_merge(array($name=>$value)));
return $arr;
}else if(is_array($value)){
foreach ($value as $elem){
$arr = array_merge($arr,$this->Convert($arr,$elem));
}
return array($name=>($arr));
}
else if(is_object($value)){
$arr = array_merge(array($name => $this-
>Convert($arr,$value)));
}
return $arr;
}
}
算法在哪里出错了?
更新
新的HTML树
$tree = new HTMLTree();
$newArr = $tree->ConvertToArray(new HTMLElement(
"table", array_merge(
array(
new HTMLElement("tr", array(
new HTMLElement("th", "Name"),
new HTMLElement("th", "Age"),
new HTMLElement("th", "City")
)
),
new HTMLElement("tk", array(
new HTMLElement("th", "Name"),
new HTMLElement("th", "Age"),
new HTMLElement("th", "City")
)
)
)
)));
更新2
$tree = new HTMLTree();
$newArr = $tree->ConvertToArray(new HTMLElement(
"table",
array(
new HTMLElement("tr", array(
new HTMLElement("th", "Name"),
new HTMLElement("th", "Age"),
new HTMLElement("th", "City")
)
)
,
new HTMLElement("tr", array(
new HTMLElement("td", "sjs"),
new HTMLElement("td", "sss"),
new HTMLElement("td", "ddd")
)
)
)));
预期的数组结构
Array (
[table] => Array (
[0] => Array (
[tr] => Array (
[0] => Array (
[td] => Name
)
[1] => Array (
[td] => Age
)
[2] => Array (
[td] => City
)
)
)
[1] => Array (
[tk] => Array (
[0] => Array (
[td] => Name
)
[1] => Array (
[td] => Age
)
[2] => Array (
[td] => City
)
)
)
)
)
答案 0 :(得分:1)
在array_merge之后,你必须将数组包装成与其他数据一致。
private function Convert($arr, HTMLElement $element){
$name = $element->getName();
$value = $element->getValue();
if(is_string($value)){
$arr = array(array_merge(array($name=>$value)));
return $arr;
}else if(is_array($value)){
foreach ($value as $elem){
$arr = array_merge($arr,$this->Convert($arr,$elem));
}
if (!isset($arr[0])) $arr = array($arr); // <<< Code modified here
return array($name=>($arr));
}
else if(is_object($value)){
$arr = array_merge(array($name => $this->Convert($arr,$value)));
}
return $arr;
}
输出:
Array (
[table] => Array (
[0] => Array (
[tr] => Array (
[0] => Array (
[td] => Name
)
[1] => Array (
[td] => Age
)
[2] => Array (
[td] => City
)
)
)
)
)
答案 1 :(得分:1)
对我来说更简单,如果toArray()
是HTMLElement,你可以在HTMLElement类中创建一个新方法$value
,递归调用。
不再需要array_merge()
。
这是完整的程序:
class HTMLElement
{
private $name;
private $value;
function __construct($name,$value) {
$this->name = $name;
$this->value = $value;
}
public function getName(){
return $this->name;
}
public function setName($name){
$this->value = $name;
}
public function getValue(){
return $this->value;
}
public function setValue($value){
$this->value = $value;
}
public function toArray() {
// Handle scalar values
$val = $this->value ;
// Handle HTMLElement values
if (is_object($this->value)) {
$val = $this->value->toArray();
}
// Handle array values :
elseif (is_array($val)) {
foreach ($val as &$item) {
if (is_object($item)) {
$item = $item->toArray();
}
}
}
// Return Key/Value pair
return [$this->name => $val] ;
}
public static function ConvertToHTMLElement(HTMLElement $obj){
return new HTMLElement($obj->name,$obj->value);
}
}
$elm = new HTMLElement(
"table", array(
new HTMLElement("tr", array(
new HTMLElement("th", "Name"),
new HTMLElement("th", "Age"),
new HTMLElement("th", "City")
)
),
new HTMLElement("tr", array(
new HTMLElement("td", "sjs"),
new HTMLElement("td", "sss"),
new HTMLElement("td", "ddd")
)
)
)
);
$newArr = $elm->toArray() ;
print_r($newArr);
输出:
Array (
[table] => Array (
[0] => Array (
[tr] => Array (
[0] => Array (
[th] => Name
)
[1] => Array (
[th] => Age
)
[2] => Array (
[th] => City
)
)
)
[1] => Array (
[tr] => Array (
[0] => Array (
[td] => sjs
)
[1] => Array (
[td] => sss
)
[2] => Array (
[td] => ddd
)
)
)
)
)