将php中的嵌套集转换为不带深度参数的嵌套数组
给定一个数组:将php中的嵌套集转换为不带深度参数的嵌套数组,php,recursion,nested-sets,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 => arr
$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
)
);
它代表了这种结构:
<label> [0,9]
<label /> [1,2]
<label> [3,8]
<label /> [4,5]
<label /> [6,7]
</label>
</label>
通过如下函数调用:
// $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' );
到目前为止,我已经尝试了这种格式,假设$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
}
}
}
}
如果有任何进一步的建议,我们将不胜感激。这应该是可行的
$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;
下一个要素:
堆栈上有两个元素,但它们都有更大的close
属性,然后是$arrValues['open']
,所以我们在循环时跳过。然后在导航部分:
指向$link
$arraySet
- 然后转到
$arraySet[0][“children”]
- 然后转到
$arraySet[0][“children”][0][“children”]
等等……我将把这个通读几遍——我不确定到底发生了什么。那我就回来!我删除了多余的if并添加了一些注释我仍然无法遵循这个示例:(我对算法添加了一些解释。我的英语不是很好,所以如果你有任何问题,请提问。嗨:)我仍然不理解它是如何工作的,但它确实工作,并且干净地解决了我的问题。如果有人能帮上忙的话,这似乎是简洁而有效的。谢谢
$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;
('index' => 0, 'close' => 10)
('index' => 0, 'close' => 8)