我有几组相同类型的 [Y、M、D],我正在尝试编写一个函数来搜索这些集合并返回适合我的参数的可用集合数组。
ReturnedSets = return_matches(Y,M,D);
我希望函数 return_matches 的三个参数是可选的。这意味着可以使用值的任意组合来返回集合。
例如,可以写 - return_matches(13,null,2); - 该函数将查找包含 [13, anyValue, 2] 的所有集合;
我正在用 PHP 编写此代码,以允许用户管理我网站上过时的文件,但我希望能够再次将此功能用于其他用途。
谢谢!
编辑:(这个,或者这个的变体,是我到目前为止所能想到的......还有一些我不明白的东西,因为这个函数最终/不会返回包含的集合y 和 d,但 m 是任意的。
if(y == s[0]){
if(m == s[1]){
if(d == s[2]){
print "day match";
}
} else {print "month match";}
} else {print "year match";}
} else {print "no match";}
最佳答案
我已经有很长时间没有用 php 写东西了,所以我会用伪代码建议你,我在想什么:
$ReturnedSets = array();
if( (!isset y) or == s[0]) {
if( (!isset m) or == s[1]) {
if( (!isset d) or == s[2]) {
$ReturnedSets[] = s;
} else {print "month match";}
} else {print "year match";}
} else {print "no match";}
return $ReturnedSets;
我希望这个伪代码对您有用,并为您提供如何获得工作代码的线索。
关于arrays - 集合的完全和部分匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5308841/