给定一个数组:
$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>
我正在尝试获取以这种格式生成的数组:
$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()
)
)
)
)
)
);
通过这样的函数调用:
// $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;
我跳过了参数化的打开和关闭标签,但您可以简单地添加它。
编辑
这里发生了什么:
首先 $stack 是空的,所以我们跳过 while()。
然后我们将对 $arraySet 的引用分配给 $link,因为 $stack 是空的,我们将第一个元素推送到 $link 是对 $arraySet 的引用。 array_push()返回 1 因为这是 $arraySet 的新长度
接下来我们将一个元素添加到$stackwhit values('index' => 0, 'close' => 10)`
下一个元素:
现在 $stack 有 1 个元素,但是 $stack[0]['close'] 大于 $arrValues['open']元素所以我们跳过 while。
我们再次将对 $arraySet 的引用设置为 $link 但现在 $stack 中有一个元素,所以我们分配 $link[$ stack[0]['index']]["children"] 引用 $link 所以现在 $link 指向 $arraySet[0]["children"] 。
现在我们将元素推送到这个子数组。 $tmp 为我们提供了这个子数组的大小,我们将适当的元素压入堆栈。
下一个元素: 它看起来和第二个一模一样,但一开始我们从堆栈中弹出一个元素。所以在堆栈上的这次迭代之后是两个元素
('index' => 0, 'close' => 10)
('index' => 0, 'close' => 8)
下一个元素:
堆栈上有两个元素,但它们都具有比 $arrValues['open'] 更大的 close 属性,因此我们跳过 while 循环。然后在导航部分:
$link 指向 $arraySet$arraySet[0]["children"] $arraySet[0]["children"][0]["children"]然后我们将元素压入这个数组。 等等……
关于php - 将php中的嵌套集转换为没有深度参数的嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14675630/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/