查找数组的数组,按顺序 [英] Find array in array, in sequence
问题描述
如何code了 [找到阵列式阵列]
功能?
的 Psuedo- code 的
草垛
阵列(0 = A,1 = B,2 = 3 = B,4 = C,5 = C,6 = A,7 = B,8 = D, 9 = C,10 =一,11 = b,12 =一,13 = b,14 = C);
针
阵列(A,B,C);
返回
阵列(阵列(2,3,4),阵列(12,13,14))
期望:
从草堆匹配针的键。以上应该给2场比赛:
- 比赛=草垛2-4
- 比赛=草垛12-14
这不应该找到AB
,ABD
也不驾驶室
等,
只有在每针价值的实例 - 按照指定的顺序
我想使它成为一个功能,这样我就可以反复运行它(我有很多这些模式)。
我试着嵌套foreachs这样做,并带动自己与专柜等果仁
我得到一个特定的点,一时无法分开的非匹配匹配。
(惊讶有没有在功能上建? in_array
和 array_intersect
似乎是个人价值而已,没有收藏?)
$大海捞针=阵列('A','B','A','B','C','D','A','B',' C');
$针=阵列('一个','B','C');$ COUNT小时=计数($草堆);回声$ COUNT小时< BR />中。
$ COUNTN =计数($针);回声$ COUNTN< BR />中。
$匹配='';
的foreach($大海捞针为$键1 => $ haystackval){
的foreach($针为$键2 => $ needleval){
$失败='0';
//如果(in_array($ needleval,$草垛)){
如果($ KEY2 [$ needleval] === $ haystackval&放大器;&安培; $失败==='0'){
回声得到needleval - $ needleval< BR />中;
}
其他{$失败='1';
}
}
}
我在创建这个函数的尝试;
函数find_array_in_array($针,干草堆$){
$键= array_keys($大海捞针,$针[0]);
$ OUT =阵列();
的foreach($键为$键){
$添加= TRUE;
$结果=阵列();
的foreach($针为$ I => $值){
如果(!(使用isset($大海捞针[$键+ $ i])及和放大器; $大海捞针[$键+ $ i] == $值)){
$ = false添加;
打破;
}
$结果[] = $键+ $ I;
}
如果($加==真){
$出[] = $结果;
}
}
返回$出;
}$草垛=阵列('A','B','A','B','C','C','A','B','D','C','A',' b','A','b','C');$针=阵列('一个','B','C');的print_r(find_array_in_array($针,干草堆$));
输出;
阵列
(
[0] =>排列
(
[0] => 2
[1] => 3
[2] => 4
) [1] =>排列
(
[0] => 12
[1] => 13
[2] => 14
))
How to code up a [find-array-in-array]
function?
Psuedo-code
Haystack:
array(0=a, 1=b, 2=a, 3=b, 4=c, 5=c, 6=a, 7=b, 8=d, 9=c, 10=a, 11=b, 12=a, 13=b, 14=c);
Needle:
array(a, b, c);
Return:
array ( array (2, 3, 4), array(12, 13, 14) )
Desired: The Keys from Haystack that match Needle. The above should give 2 matches:
- match = Haystack 2-4
- match = Haystack 12-14
It should not find "a b"
, "a b d"
nor "c a b"
etc.,
only instances of each value in Needle - in the specified order.
I'd like to make it a function so I can run it repeatedly (I have lots of these patterns).
I've tried doing this with nested foreachs, and driven myself nuts with counters etc.
I get to a certain point, and am unable to separate matches from non-matches.
(Surprised there isn't a built in function? in_array
and array_intersect
seem to be for individual values only, not collections?)
$haystack = array('a','b','a','b','c','d','a','b','c');
$needle = array('a','b','c');
$CountH = count($haystack); echo $CountH."<br/>";
$CountN = count($needle); echo $CountN."<br/>";
$matches ='';
foreach ($haystack as $key1=>$haystackval){
foreach ($needle as $key2=>$needleval) {
$fail = '0';
//if (in_array($needleval, $haystack)) {
if ($key2[$needleval] === $haystackval && $fail === '0') {
echo "Got needleval - ".$needleval ."<br/>";
}
else { $fail='1';
}
}
}
My attempt at creating this function;
function find_array_in_array($needle, $haystack) {
$keys = array_keys($haystack, $needle[0]);
$out = array();
foreach ($keys as $key) {
$add = true;
$result = array();
foreach ($needle as $i => $value) {
if (!(isset($haystack[$key + $i]) && $haystack[$key + $i] == $value)) {
$add = false;
break;
}
$result[] = $key + $i;
}
if ($add == true) {
$out[] = $result;
}
}
return $out;
}
$haystack = array('a', 'b', 'a', 'b', 'c', 'c', 'a', 'b', 'd', 'c', 'a', 'b', 'a', 'b', 'c');
$needle = array('a', 'b', 'c');
print_r(find_array_in_array($needle, $haystack));
Outputs;
Array
(
[0] => Array
(
[0] => 2
[1] => 3
[2] => 4
)
[1] => Array
(
[0] => 12
[1] => 13
[2] => 14
)
)
这篇关于查找数组的数组,按顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!