MyS*_*eam 3 php recursion nested-sets
给定一个数组:
$arrData = array(
0 => array (
'uid' => 1,
'name' => 'label',
'open' => 0,
'close' => 9
),
1 => array (
'uid' => 2,
'name' => 'label',
'open' => 1,
'close' => 2
),
2 => array (
'uid' => 3,
'name' => 'label',
'open' => 3,
'close' => 8
),
3 => array (
'uid' => 4,
'name' => 'label',
'open' => 4,
'close' => 5
),
4 => array (
'uid' => 5,
'name' => 'label',
'open' => 6,
'close' => 7
)
);
Run Code Online (Sandbox Code Playgroud)
代表这种结构:
<label> [0,9]
<label /> [1,2]
<label> [3,8]
<label /> [4,5]
<label /> [6,7]
</label>
</label>
Run Code Online (Sandbox Code Playgroud)
我试图找到一个导致这种格式的数组:
$arrNesting = array(
0=>array(
'item' => array('uid'=>1, 'name'=>'label', 'open'=>0, 'close'=>9),
'children' => array(
0=>array(
'item' => array('uid'=>2, 'name'=>'label', 'open'=>1, 'close'=>2),
'children' => array()
),
1=>array(
'item' => array('uid'=>3, 'name'=>'label', 'open'=>3, 'close'=>8),
'children' => array(
0=>array(
'item' => array('uid'=>2, 'name'=>'label', 'open'=>4, 'close'=>5),
'children' => array()
),
1=>array(
'item' => array('uid'=>3, 'name'=>'label', 'open'=>6, 'close'=>7),
'children' => array()
)
)
)
)
)
);
Run Code Online (Sandbox Code Playgroud)
通过这样的函数调用:
// $arrData: Source Data with keys for denoting the left and right node values
// 'open': the key for the left node's value
// 'close': the key for the right node's value
$arrNested = format::nestedSetToArray( $arrData, 'open', 'close' );
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试过这种格式,假设$ arrData值始终是左值升序.uid值"恰好"也是有序的,但不依赖于:
public static function nestedSetToArray( $arrData = array(), $openKey='open', $closeKey='close') {
// Hold the current Hierarchy
$arrSets = array();
// Last parent Index, starting from 0
$intIndex = 0;
// Last Opened and Closed Node Values, and maximum node value in this set
$intLastOpened = 0;
$intLastClosed = null;
$intMaxNodeNum = null;
// loop through $arrData
foreach( $arrData as $intKey=>$arrValues) {
if( !isset( $arrSets[ $intIndex ] )) {
// Create a parent if one is not set - should happen the first time through
$arrSets[ $intIndex ] = array ('item'=>$arrValues,'children'=>array());
$intLastOpened = $arrValues[ $openKey ];
$intLastClosed = null; // not sure how to set this for checking 2nd IF below
$intMaxNodeNum = $arrValues[ $closeKey ];
} else {
// The current item in $arrData must be a sibling or child of the last one or sibling of an ancestor
if( $arrValues[ $openKey ] == $intLastOpened + 1) {
// This is 1 greater then an opening node, so it's a child of it
} else if( /* condition for sibling */ ) {
// This is 1 greater than the intLastClosed value - so it's a sibling
} else if( /* condition for sibling of ancestor */ ) {
// This starts with a value greater than the parent's closing value...hmm
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
任何指向进一步采取这一点将不胜感激.
这应该有效
$stack = array();
$arraySet = array();
foreach( $arrData as $intKey=>$arrValues) {
$stackSize = count($stack); //how many opened tags?
while($stackSize > 0 && $stack[$stackSize-1]['close'] < $arrValues['open']) {
array_pop($stack); //close sibling and his childrens
$stackSize--;
}
$link =& $arraySet;
for($i=0;$i<$stackSize;$i++) {
$link =& $link[$stack[$i]['index']]["children"]; //navigate to the proper children array
}
$tmp = array_push($link, array ('item'=>$arrValues,'children'=>array()));
array_push($stack, array('index' => $tmp-1, 'close' => $arrValues['close']));
}
return $arraySet;
Run Code Online (Sandbox Code Playgroud)
我跳过参数化的打开和关闭标签,但你可以简单地添加它.
编辑
这里发生了什么:
首先$stack是空的,所以我们跳过while().然后我们分配参考$arraySet的$link,因为$stack是空的,我们推到第一元件$link,其是参照$arraySet.array_push()return 1 because this is the new length of$ arraySet
Next we add an element to the$ stack whit values('index'=> 0,'close'=> 10)`
下一个元素:现在$stack有1个元素,但是$stack[0]['close']大于$arrValues['open']元素,所以我们跳过.我们再次设定参考$arraySet到$link,但现在有件上$stack,所以我们指定$link[$stack[0]['index']]["children"]参考$链接所以现在$link指向$arraySet[0]["children"].现在我们将元素推送到这个子数组.$tmp给出了这个子数组的大小,我们将适当的元素推送到堆栈.
下一个元素:它看起来与第二个元素完全相同,但在开始时我们从堆栈中弹出一个元素.所以在堆栈上迭代之后是两个元素
('index' => 0, 'close' => 10)
('index' => 0, 'close' => 8)
Run Code Online (Sandbox Code Playgroud)
下一个元素:堆栈上有两个元素,但两个元素都有更大的close属性,$arrValues['open'] 所以我们跳过while循环.然后在导航部分:
$link 指着 $arraySet$arraySet[0]["children"] $arraySet[0]["children"][0]["children"]我们将元素推送到此数组.等等...
| 归档时间: |
|
| 查看次数: |
1468 次 |
| 最近记录: |