开发者

Search an array[key] for any integer but one

开发者 https://www.devze.com 2023-03-22 05:11 出处:网络
I am currently stumped while searching for an answer, but I have an array which contains information relating to a tournament, as seen below.

I am currently stumped while searching for an answer, but I have an array which contains information relating to a tournament, as seen below.

I would like to search through this Array of arrays for specifically the opponent key, and determine if they are all -2. This is because the logic is that -2 is a bye, so if all people have -2 as their opponent, this means that all brackets have been finished.

My initial thought was just do an array_search(!(-2), $anArray), but that obviously didn't work and I hit myself on the head for thinking it would be that easy haha. Thanks.

EX:

Array ( 
    [0] => Array ( [dId] => 11 [uId] => 3 [id] => 1 [round] => 0 [opponent] => 3 [bracket] => 0 )
    [1] => Array ( [dId] => 11 [uId] => 5 [id] => 2 [round] => 0 [opponent] => -2 [bracket] => 1 )
    [2] => Array ( [dId] => 11 [uId] => 10 [id] => 3 [round] => 0 [opponen开发者_如何学运维t] => 1 [bracket] => 0 ) )


This function will return true if all opponents are -2 and false if there is a single opponent != -2:

function all_opponent_search($arr){
    for($i = 0; $i < count($arr); $i++)
        if($arr[$i]['opponent'] != -2)
            return false;
    return true;
}


how about:

foreach ($masterArray as $subArray) {
    if (isset($subArray['opponent']) && ($subArray['opponent'] == -2)) {
        // do whatever you need
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消