简体   繁体   English

检查每个值是否在不同的 arrays

[英]Check if each values are in different arrays

I need to check if element 'a' is present twice, 'b' is present twice and 'c' is present once in a group of arrays.我需要检查元素“a”是否出现两次,“b”是否出现两次,“c”是否出现在一组 arrays 中。 Each element should be in five different arrays.每个元素应该在五个不同的 arrays 中。

That is like, 'a' in array1, another 'a' in array2, 'b' in array3, another 'b' in array4 and 'c' in array5.就像,array1中的'a',array2中的另一个'a',array3中的'b',array4中的另一个'b'和array5中的'c'。 there should be atleast five or more arrays and each element should be in different arrays using php.应该有至少五个或更多 arrays 并且每个元素应该使用 php 位于不同的 arrays 中。 Now my code is现在我的代码是

$arr = array(
    $branch1 = array('a', 'b'),
    $branch2 = array('b','c'),
    $branch3 = array('a',  'c'),
    $branch4 = array('c', 'a'),
    $branch5 = array('b', 'a'),
    $branch6 = array('b', 'c', 'a')
    );//This may have any number of branches and any kind of combinations of a, b and c(but each element only once in each array). 

$reqd_branch_count = 5;//required branch count

As I am new to php, now I have written a very long code, but it fails when trying new combinations.Please help me if someone knows.由于我是 php 的新手,现在我写了一个很长的代码,但是在尝试新的组合时它失败了。如果有人知道,请帮助我。


From asker's comment:来自提问者的评论:

The condition is a user has some ranks in his/her downline which are in branches.条件是用户在他/她的下线中有一些等级,这些等级位于分支中。 for example rank1, rank2 and rank3,.... Those rank counts are shown in that array.例如 rank1、rank2 和 rank3,.... 这些排名计数显示在该数组中。 If that user need to get rank8 he/she should have one rank 7, two rank 5 or 7s, and two rank4s, that should be 1+2+2=5 branches, that is each rank should be in different branches.如果该用户需要获得rank8,他/她应该有一个rank 7,两个rank 5或7s,以及两个rank4,应该是1 + 2 + 2 = 5个分支,即每个rank应该在不同的分支中。 Hope you understood the question希望你明白这个问题

if i understood correctly如果我理解正确

merge all arrays, count amount of all items in all arrays and test you want合并所有 arrays,计算所有 arrays 中所有项目的数量并测试你想要的

$arr = array_merge($branch1,$branch2,$branch3,$branch4,$branch5,$branch6);
$count = array_count_values($arr);

echo $count[7]; // 4
echo $count[4]; // 2

so, yuo can make condition所以,你可以提出条件

if (($count[7] == 1)  or ($count[7] == 2) or ($count[5] == 2) or ($count[4] == 2))  
   {  any stuff for true}

UPDATE更新

$arr = array(
$branch1=array('3'),
$branch2=array('4'),
$branch3=array('3','5','7'),
$branch4=array('3','4','7'),
$branch5=array('7'),
$branch6=array('4','7'));
// find rank per branch
$ranks = array_map('max', $arr);
// make array rank => amount
$count = array_replace(array_fill(0,7,0), array_count_values($ranks));

if (($count[7] >= 1) and (($count[7] + $count[5]) >= 2) and (($count[7] + $count[5] + $count[4]) >= 5)) { 
     echo "Satisfy ";
}

Because it is another question, this is another answer因为这是另一个问题,所以这是另一个答案

$arr = array(
    $branch1 = array('a'),
    $branch2 = array('b','c'),
    $branch3 = array('a',  'c'),
    $branch4 = array('c', 'a'),
    $branch5 = array('a'),
    $branch6 = array( 'c', 'a')
    );

$names = array('a', 'b', 'c');    // for convenience only 
var_dump(goNext($arr, $names));   // watch result

function goNext($arr, $names) {
    // for each name make array with list of brances where it is
    $in = array(array(), array(), array());
    foreach($arr as $k1 => $branch) 
        foreach($names as $k2 => $letter)
            if(in_array($letter, $branch)) $in[$k2][] = $k1;

    foreach ($in[0] as $i1)  // 1st a
        foreach (array_diff($in[0], array($i1)) as $i2)   // 2nd a
            foreach (array_diff($in[1], array($i1,$i2)) as $i3)  // 1st b
                foreach (array_diff($in[1], array($i1,$i2,$i3)) as $i4)  // 2nd b
                    foreach(array_diff($in[2], array($i1,$i2,$i3,$i4)) as $i5) {
                        // if here we find combination we need
                        // next line only for debug
                        // it shows set of branches that give true
                        //    a           a           b           b           c
                        echo $i1 . " " . $i2 . " " . $i3 . " " . $i4 . " " . $i5;
                        return(true);
                    }
    return(false);   // combination has not found
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM