我正在使用 PHP,我需要帮助来完成一项看似简单的数组任务。
这是我的示例数组:
$arr = array(
0 => NULL,
1 => NULL,
2 => NULL,
3 => NULL,
8 => '2',
9 => '2',
10 => '2',
11 => '2',
12 => '3',
13 => '3',
14 => '8',
15 => '8',
16 => '14',
17 => '14',
18 => '14'
);
数组的键代表ID(唯一)。
这些值是 parentIDs,即父“节点”的 ID。 NULL 表示没有 parentID(即新数组的第一个维度)。
现在,我需要创建一个新的多维数组,其中包含所有子元素的父 ID。 (这听起来可能很困惑,抱歉我缺乏描述能力。下面有一个例子,应该更清楚)
下面是我的示例的新数组在应用“排序”函数或您称之为任何函数之后的样子:
$arr = array(
0 => array(),
1 => array(),
2 => array(
8 => array(
14 => array(
16 => array(),
17 => array(),
18 => array()
),
15 => array()
),
9 => array(),
10 => array(),
11 => array()
),
3 => array(
12 => array(),
13 => array()
)
);
我知道所有的空数组()可能不是一个非常干净和优雅的解决方案,但不幸的是,这就是我需要的方式!
最佳答案
此递归函数会将给定的数据添加到正确的父级,并且应该为起始数组中的每个元素调用一次。
function add_branch(&$tree, $datum, $parent) {
// First we have the base cases:
// If the parent is NULL then we don't need to look for the parent
if ($parent == NULL) {
$tree[$datum] = array();
return true;
}
// If the array we've been given is empty, we return false, no parent found in this branch
if (! count($tree)) {
return false;
}
// We loop through each element at this level of the tree...
foreach($tree as $key => $val) {
// If we find the parent datum...
if ($key == $parent) {
// We add the new array in and we're done.
$tree[$key][$datum] = array();
return true;
}
// Otherwise, check all the child arrays
else {
// Now we check to see if the parent can be found in the curent branch
// If a recursive call found a parent, we're done
if (add_branch($tree[$key], $datum, $parent)) {
return true;
}
}
}
// If none of the recursive calls found the parent, there's no match in this branch
return false;
}
评论比较啰嗦,希望大家看明白是怎么回事。我鼓励您阅读一些有关递归函数的文章,以了解它。
这是它的用法:
$arr = array(
0 => NULL,
1 => NULL,
2 => NULL,
3 => NULL,
8 => '2',
9 => '2',
10 => '2',
11 => '2',
12 => '3',
13 => '3',
14 => '8',
15 => '8',
16 => '14',
17 => '14',
18 => '14'
);
$final = array();
foreach ($arr as $datum => $parent) {
add_branch($final, $datum, $parent);
}
$final 现在有正确的结束数组,如问题中所示。
关于php - 创建多维数组的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4238787/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我有多个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上找到一
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife