在foreach循环执行php之前检查条件

时间:2014-09-24 04:56:19

标签: php mysql

我想在循环执行之前检查条件

这是我的数组

$rs=Array (    
    [0] => Array ( 
               [questionID] => 47
               [surveyID] => 51
               [userID] => 31 
               [question_Title] => Choose Any One? 
               [question_Type] => Dropdown 
               [response] => 1.Android 2.Windows 3.Blackberry 
               [required] => 0 
               [add_time] => 0
    )
    [1] => Array ( 
               [questionID] => 48 
               [surveyID] => 51 
               [userID] => 31 
               [question_Title] => Is it? 
               [question_Type] => Bigbox 
               [response] => Yes No 
               [required] => 1 
               [add_time] => 0 
    )    
    [2] => Array ( 
               [questionID] => 129 
               [surveyID] => 51 
               [userID] => 31 
               [question_Title] => sELECT 
               [question_Type] => Single 
               [response] => DFG HBK GHCK HK 
               [required] => 0 
               [add_time] => 0 
    )
) 

现在我想检查$ rs [required] => 1 然后停止所有执行或$ rs遍历使用每个循环 而是从上面的例子我也想停止执行第一个循环。

1 个答案:

答案 0 :(得分:0)

使用array_column并实现此功能

<强>参考

低版本数组列功能代码 https://github.com/ramsey/array_column/blob/master/src/array_column.php

 <?php

    //assumed records is your array
    $required = array_column($records, 'required');


    if(FALSE===array_search('1', $required))
    {
        //value is not exist then process your loop 
    }
    else
    {
        //required value is 1 then ignore the loop
    }