How to check if it is the first time a value is found in if statement inside a foreach iteration

前端 未结 1 1735
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-27 00:40

Suppose I have an array.

$array1 = array(\'John\',\'Mike\',\'Tim\',\'Dan\');

Using a foreach iteration, I\'m looping through $ar

相关标签:
1条回答
  • 2021-01-27 01:22

    Use a variable to keep that information in

    $firstTime = true;
    
    foreach ($array1 as $key => $value) {
        $dbQueryResult = true; // or false depending on iteration
        if($dbQueryResult === true){
            // HOW DO I CHECK IF IT IS THE FIRST TIME I'M GETTING HERE
            if ( $firstTime ) {
                // do first time stuff
    
                $firstTime = FALSE;
            }
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题