更新:
$categoryArr=array(array("parentid"=>1,"title"=>"Father 1","childof"=>0),array("parentid"=>2,"title"=>"Son 1.2","childof"=>1),array("parentid"=>3,"title"=>"Grandson 1.2.3","childof"=>2),array("parentid"=>4,"title"=>"Son 1.4","childof"=>1),array("parentid"=>5,"title"=>"Son 1.5","childof"=>1),array("parentid"=>6,"title"=>"Son 1.6","childof"=>1),array("parentid"=>7,"title"=>"Grandson 1.5.7","childof"=>5),array("parentid"=>8,"title"=>"Father 8","childof"=>0),array("parentid"=>9,"title"=>"Son 8.9","childof"=>8),array("parentid"=>10,"title"=>"Grandgrandson 1.2.3.10","childof"=>3));
function rebuild($data,$find)
{
$cache=array_filter($data,create_function('$n','return $n["childof"]=='.$find.';'));
$rt=array();
foreach($cache as $node)
{
$rt[]=$node;
$rt=array_merge($rt,rebuild($data,$node["parentid"]));
}
return $rt;
}
$cache=array_filter($categoryArr,function($n){return $n["childof"]==0;});
$base=array();
foreach($cache as $root)
{
$base[]=array_merge(array($root),rebuild($categoryArr,$root["parentid"]));
}
$xml=new DOMDocument("1.0","UTF-8");
$xml->formatOutput=true; // just to debug
$root=$xml->createElement("root");
$xml->appendChild($root);
foreach($base as $list)
{
$table=$xml->createElement("table");
$root->appendChild($table);
$rows=ceil(count($list)/4);//change 4 to whatever column you need
foreach(range(1,$rows) as $idx)
{
$tr=$xml->createElement("tr");
$table->appendChild($tr);
}
$cur=0;
foreach($list as $node)
{
if($cur>=$rows) $cur=0;
$td=$xml->createElement("td");
$td->appendChild($xml->createTextNode($node["title"]));
$table->getElementsByTagName("tr")->item($cur)->appendChild($td);
$cur++;
}
}
echo $xml->saveXML();
輸出:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<table>
<tr>
<td>Father 1</td>
<td>Grandson 1.2.3</td>
<td>Son 1.4</td>
<td>Grandson 1.5.7</td>
</tr>
<tr>
<td>Son 1.2</td>
<td>Grandgrandson 1.2.3.10</td>
<td>Son 1.5</td>
<td>Son 1.6</td>
</tr>
</table>
<table>
<tr>
<td>Father 8</td>
<td>Son 8.9</td>
</tr>
</table>
</root>
附:使用DOMDocument
不是必須的;既然我們已經知道每個表有多少項目,有可能只是計算每個表格單元格,並直接輸出它的位置,但對我來說,DOMDocument
是懶惰:)
第一次嘗試:
可能不是一個完美的解決方案,但它應該工作:
$categoryArr=array(array("parentid"=>1,"title"=>"Father","childof"=>0),array("parentid"=>2,"title"=>"Son 2","childof"=>1),array("parentid"=>3,"title"=>"Grandson 2.3","childof"=>2),array("parentid"=>4,"title"=>"Son 4","childof"=>1),array("parentid"=>5,"title"=>"Son 5","childof"=>1),array("parentid"=>6,"title"=>"Son 6","childof"=>1),array("parentid"=>7,"title"=>"Grandson 5.6","childof"=>5));
$xml=new DOMDocument("1.0","UTF-8");
$xml->formatOutput=true; // just to debug
$root=$xml->createElement("table");
$xml->appendChild($root);
foreach(range(1,ceil(count($categoryArr)/4)) as $idx) // change 4 to whatever column you need
{
$tr=$xml->createElement("tr");
$root->appendChild($tr);
}
function addNode(&$xml,$data,$currID,&$currRow)
{
if($currRow>=$xml->getElementsByTagName("tr")->length) $currRow=0;
$cache=array_filter($data,create_function('$n','return $n["parentid"]=='.$currID.';'));
if(count($cache)==1)
{
$td=$xml->createElement("td");
$cache=array_pop($cache);
$td->appendChild($xml->createTextNode($cache["title"]));
$xml->getElementsByTagName("tr")->item($currRow)->appendChild($td);
$currRow+=1;
}
$cache=array_filter($data,create_function('$n','return $n["childof"]=='.$currID.";"));
foreach($cache as $node)
{
addNode($xml,$data,$node["parentid"],$currRow);
}
}
$cur=0;
addNode($xml,$categoryArr,0,$cur);
echo $xml->saveXML();
輸出:
<?xml version="1.0" encoding="UTF-8"?>
<table>
<tr>
<td>Father</td>
<td>Grandson 2.3</td>
<td>Son 5</td>
<td>Son 6</td>
</tr>
<tr>
<td>Son 2</td>
<td>Son 4</td>
<td>Grandson 5.6</td>
</tr>
</table>
沒有「真實」的數據要測試,所以讓我知道是否有什麼問題。
您的PHP代碼提示可能有更多的2個級別,但您的預期輸出只有2(似乎不可擴展)。你必須更具體。 – Passerby
@Passerby:是的,可能會有更多關卡。說,類別A - > child1 - > Child1.1 e.t.c.無論如何,這應該不是問題,因爲我們將開始從第一級/類別創建表。 – mustafa
你的例子中應該如何顯示孫子(和更深層次)還不清楚;例如'父親:[兒子1,兒子2,兒子3]'預計顯示爲'父親兒子2兒子3',但父親:[兒子1:[孫子1,孫子2,孫子3],兒子2,兒子3:[孫子4,孫子5] ]'? – Passerby