想了解php–如何列出树的所有部分树的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于php树形结构怎么遍历出来的相关问题,此外,我们还将为您介绍关于linux–如何列出存储库中的所有可用包、
想了解php – 如何列出树的所有部分树的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于php树形结构怎么遍历出来的相关问题,此外,我们还将为您介绍关于linux – 如何列出存储库中的所有可用包、perl – 如何列出在给定范围内的所有变量?、php – 如何列出两个日期之间的所有月份、php-如何列出Laravel嵌套表中的所有项目的新知识。
本文目录一览:- php – 如何列出树的所有部分树(php树形结构怎么遍历出来)
- linux – 如何列出存储库中的所有可用包
- perl – 如何列出在给定范围内的所有变量?
- php – 如何列出两个日期之间的所有月份
- php-如何列出Laravel嵌套表中的所有项目
php – 如何列出树的所有部分树(php树形结构怎么遍历出来)
我不想列出数组中的所有排列–Get all permutations of a PHP array?
我不想从数组中按顺序找到所有组合 – https://stackoverflow.com/a/38871855/1260548
以上两个例子让我到了现在的位置,但我仍然会产生太多的组合.有50个节点,如果不是数万亿个组合,我最终会得到数十亿个,我想我可以通过树形结构进一步减少这一点.
我正在寻找的是树的所有可能的有序组合,可以像这样结构化为多维数组
[1] --[2] --[4] [8] --[3] --[9] ----[5] [6] [7]
我想要找到的是所有可能的开放节点(甚至叶子/端节点都可以打开).所以这里有一个可能的组合就是所有的数字
> 1.2.3.4.5.8.9
这里的节点1是2和4的父节点.8是3和9的父节点.9是8的子节点,但父节点是5.其他可能的组合是.
> 1
> 1.2.4
> 1.6.7.8
> 3.5.8.9
> 3.5.6.7.8.9
如果父节点未打开,则无法打开节点.例如,如果不包括1,则不能包括2和4.如果不包括9,则不包括5,如果不包括8,则不包括3,9和5.
以下是我用来生成要测试的样本节点结构的代码.请注意,这个结构是有序的并且具有固定的深度,因为我想要提出一个可以处理任何顺序和任何深度的功能.
$arr = []; $result = []; $xtotal = 0; $ytotal = 0; $ztotal = 0; for ($x=0; $x<2; $x++) { $arr[$xtotal] = array(); $ytotal = $xtotal+1; for ($y=0; $y<2; $y++) { $arr[$xtotal][$ytotal] = array(); $ztotal = $ytotal+1; for ($z=0; $z<2; $z++) { $arr[$xtotal][$ytotal][$ztotal] = array(); $ztotaL++; } $ytotal = $ztotal+1; } $xtotal = $ytotal+1; } for ($c=0; $c<5; $c++) { $arr[$xtotal] = array(); $xtotaL++; }
所以我想知道如何编写一个列出所有这些可能组合的函数?
编辑:使用较小的集合,我可以列出所有可能的组合.
[1] --[2] --[4] [8] 1 8 1.8 1.2 1.4 1.2.8 1.4.8 1.2.4 1.2.4.8
解决方法
我使用了一个不同的节点生成器(虽然我也测试了你的),这使我可以更灵活地创建随机组合:
$arr = []; $counter = 0; // you can change the (2,6) to produce different numbers of root nodes for($i=1;$i<=mt_rand(2,6);$i++){ $curr = $counter++; $arr[$curr] = []; // guarantee the first node (0) will have children nodes (easier testing) - random for other nodes $child = ($curr == 0) ? true : rand(0,1); if($child){ // you can change the (1,2) for($j=1;$j<=mt_rand(1,2);$j++){ $curr2 = $counter++; $arr[$curr][$curr2] = []; $child2 = rand(0,1); if($child2){ // you can change the (1,2) here too for($k=1;$k<=mt_rand(1,2);$k++){ $curr3 = $counter++; $arr[$curr][$curr2][$curr3] = []; } } } } }
现在计算:
function treenodes($arr,&$results,$parent=null){ foreach($arr as $k=>$a){ // here we copy all our current results - this gives us one with the current node closed (original),and one with it open (clone) $clone = []; foreach($results as $key=>$result){ // if this node is allowed in this result (parent is on) - root nodes are always allowed if($parent === null || in_array($parent,$result)){ $clone[] = array_merge($result,array($k)); } } $results = array_merge($results,$clone); // if this node has children,run this function on them as well if(count($a)){ treenodes($a,$results,$k); } } } // we start with one option of no nodes open $results = [[]]; treenodes($arr,$results); // show results - you can order these another way if you'd like before printing print count($results)."\n"; foreach($results as $result){ print implode(",",$result)."\n"; }
linux – 如何列出存储库中的所有可用包
我正在寻找一种从中央存储库db for centos和ubuntu获取完整列表的方法,以获取相关存储库中存在的任何软件包(不仅适用于我服务器中安装的软件包).列表应包含包名称和版本.
任何的想法?
就CentOS而言,您可以使用该命令列出配置的存储库中可用的所有软件包
yum list available
如果您只对一个存储库中可用的包感兴趣,请使用该命令
yum --disablerepo "*" --enablerepo "
对于Ubuntu:
apt search your-package-name
看到所有包裹:
apt search .
perl – 如何列出在给定范围内的所有变量?
Padwalker
的peek_our和peek_my,但是如何获得所有的全局变量的名称和值,如$“和$ /?
#!/usr/bin/perl use strict; use warnings; use PadWalker qw/peek_our peek_my/; use Data::Dumper; our $foo = 1; our $bar = 2; { my $foo = 3; print Dumper in_scope_variables(); } print Dumper in_scope_variables(); sub in_scope_variables { my %in_scope = %{peek_our(1)}; my $lexical = peek_my(1); #lexicals hide package variables while (my ($var,$ref) = each %$lexical) { $in_scope{$var} = $ref; } ############################################## #FIXME: need to add globals to %in_scope here# ############################################## return \%in_scope; }
解决方法
另外看看“Mastering Perl: http://www252.pair.com/comdog/mastering_perl/
具体: http://www252.pair.com/comdog/mastering_perl/Chapters/08.symbol_tables.html
您正在查找的那些变量将在主命名空间下
Google快速搜索:
{ no strict 'refs'; foreach my $entry ( keys %main:: ) { print "$entry\n"; } }
你也可以做
*sym = $main::{"/"}
同样对于其他值
如果你想找到你可以做的符号的类型(从掌握perl):
foreach my $entry ( keys %main:: ) { print "-" x 30,"Name: $entry\n"; print "\tscalar is defined\n" if defined ${$entry}; print "\tarray is defined\n" if defined @{$entry}; print "\thash is defined\n" if defined %{$entry}; print "\tsub is defined\n" if defined &{$entry}; }
php – 如何列出两个日期之间的所有月份
我想列出两个日期之间的所有月份.
例如;开课日期是:2010-12-02,最后日期是:2012-05-06
我想列出这样的东西:
2010-12
2011-01
2011-02
2011-03
2011-04
.
.
.
2012-04
2012-05
这是我尝试过的,它根本不起作用:
$year_min = 2010;
$year_max = 2012;
$month_min = 12;
$month_max = 5;
for($y=$year_min; $y<=$year_max; $y++)
{
for($m=$month_min; $m<=$month_max; $m++)
{
$period[] = $y.$m;
}
}
解决方法:
PHP 5.3
$start = new DateTime('2010-12-02');
$start->modify('first day of this month');
$end = new DateTime('2012-05-06');
$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("Y-m") . "<br>\n";
}
See it in action
PHP 5.4或更高版本
$start = (new DateTime('2010-12-02'))->modify('first day of this month');
$end = (new DateTime('2012-05-06'))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("Y-m") . "<br>\n";
}
我们将开始和结束日期修改为月初的部分非常重要.如果我们没有,并且当前的日子高于2月的最后一天(即非闰年28天,闰年29天),这将超过2月份.
php-如何列出Laravel嵌套表中的所有项目
我正在使用Laravel的Eloquent ORM,但我在急于加载要显示的项目时遇到麻烦.
这是场景:
>用户关注博客
>博客有帖子
我有一个名为Relationships的数据库表,该表用于存储用户ID和Blog ID,以显示哪个用户关注哪个Blog.我有一个描述博客的Blog表,还有一个Posts表. Relationships表将是我的数据透视表,用于将Users和Blogs表连接在一起.现在,我需要在列表中列出用户遵循的所有博客中的所有帖子.
这是我的用户模型:
public function following() {
return $this->has_many_and_belongs_to('Blog', 'relationships', 'user_id', 'blog_id');
}
这是我的Blog模型:
public function followers() {
return $this->has_many_and_belongs_to('User', 'relationships', 'blog_id', 'user_id');
}
public function posts() {
return $this->has_many('Post');
}
这就是我试图检索列表中的帖子的方式:
$posts = User::with(array('following', 'following.posts'))
->find($user->id)
->following()
->take($count)
->get();
这段代码仅列出了实际的Blog,我需要他们的帖子.
感谢您的帮助,如果您需要更多详细信息,请告诉我.
解:
我在下面稍加修改了可接受的答案,我决定使用JOIN将sql调用的数量减少到仅1个调用.这里是:
$posts = Post::join('blogs', 'posts.blog_id', '=', 'blogs.id')
->join('relationships', 'blogs.id', '=', 'relationships.blog_id')
->select('posts.*')
->where('relationships.user_id', '=', $user->id)
->order_by('posts.id', 'desc')
->take($count)
->get();
解决方法:
这是本机口才方法无法实现的.但是您可以使用一些Fluent方法来连接这些表.例如:
在这里编辑:我已将急切加载添加到Post查询中.
$user = User::find(1);
$posts = Post::with('blog') // Eager loads the blog this post belongs to
->join('blogs', 'blogs.id', '=', 'posts.blog_id')
->join('relationships', 'relationships.blog_id', '=', 'blogs.id')
->where('relationships.user_id', '=', $user->id)
->order_by('posts.id', 'desc') // Latest post first.
->limit(10) // Gets last 10 posts
->get('posts.*');
foreach ($posts as $post) {
print($post->title);
}
例如,如果您还需要该用户关注的所有博客的列表以显示在边栏上.您可以DYI而不是依靠Eloquent,这应该更快,更可定制.例如:
$user = User::with('following')->find(1);
// This creates a dictionary for faster performance further ahead
$dictionary = array();
foreach ($user->following as $blog) {
$dictionary[$blog->id] = $blog;
}
// Retrieves latest 10 posts from these blogs that he follows
// Obs: Notice the array_keys here
$posts = Post::where_in('blog_id', array_keys($blog_ids))
->order_by('posts.id', 'desc')
->limit(10)
->get();
// Hydrates all posts with their owning blogs.
// This avoids loading the blogs twice and has no effect
// on database records. It's just a helper for views.
foreach ($posts as $post) {
$post->relationships['blog'] = $dictionary[$post->blog_id];
}
在视图上:
foreach ($user->following as $blog) {
print($blog->title);
}
foreach ($posts as $post) {
print($post->title . ' @'. $post->blog->title);
}
今天关于php – 如何列出树的所有部分树和php树形结构怎么遍历出来的分享就到这里,希望大家有所收获,若想了解更多关于linux – 如何列出存储库中的所有可用包、perl – 如何列出在给定范围内的所有变量?、php – 如何列出两个日期之间的所有月份、php-如何列出Laravel嵌套表中的所有项目等相关知识,可以在本站进行查询。
本文标签: