我有一组控件,包括名称和首先要渲染的控件列表。以下是数组:
$controls [0] = array ("name" => "combobox", array ("countryStore", "center" ) );
$controls [1] = array ("name" => "docBody", array () );
$controls [2] = array ("name" => "top", array ("docBody" ) );
$controls [3] = array ("name" => "button1", array ("formPane" ) );
$controls [4] = array ("name" => "center", array ("docBody" ) );
$controls [5] = array ("name" => "text1", array ("formPane" ) );
$controls [6] = array ("name" => "countryStore", array ("center" ) );
$controls [7] = array ("name" => "formPane", array ("center" ) );
我需要按照正确的顺序排列控件数组,这样每当我渲染一个控件时,所有这些控件都已经呈现出来。
PHP中是否有任何可以对这种关系进行排序的函数?
所需输出
$controls [0] = array ("name" => "docBody", array () );
$controls [1] = array ("name" => "top", array ("docBody" ) );
$controls [2] = array ("name" => "center", array ("docBody" ) );
$controls [3] = array ("name" => "formPane", array ("center" ) );
$controls [4] = array ("name" => "countryStore", array ("center" ) );
$controls [5] = array ("name" => "combobox", array ("countryStore", "center" ) );
$controls [6] = array ("name" => "button1", array ("formPane" ) );
$controls [7] = array ("name" => "text1", array ("formPane" ) );
排序后的代码:
$sortedArray = $SortControls($controls);
foreach ($sortedArray as $control) {
System->getControl($control['name'])->render();
}
答案 0 :(得分:4)
我不熟悉PHP,但是......
您的数据可用于定义图形,其中节点是您的实体(例如“text1”,“formPane”),它们之间的依赖关系是有向边。
如果此有向图是非循环的(DAG),那么topological sorting是一种简单的算法,可以实现您想要的排序。
该算法还可以检测周期,这意味着这种排序是不可能的。