2014-09-23 110 views
0
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 
    ) 
) 

這是我的多維檢查關聯數組包含值

現在我想最初檢查數組包含[需要] => 1或[需要] => 0

我不想遍歷數組

+0

嘗試arrayName中的[0] [ 'required'] – Swetha 2014-09-23 06:29:56

+0

你想檢查這個每個子陣列嗎?明確你的意圖 – niyou 2014-09-23 06:50:34

+0

niyou,是的!我想檢查這個每個子陣列 – 2014-09-23 07:25:54

回答

1

比方說,您的數組名稱是$myarray

所以,你可以做這樣的事情 -

foreach($myarray as $arrelement) 
{ 
    if(isset($arrelement[required]) && $arrelement[required] == 1) //choose 1 or 0 in code as per your need 
    { 
     //traverse your array and do needful 
    } 
} 
1
if($yourArr[0]['required']==0) //Just check for first element in your array 
    echo "Zero"; 
else 
    echo "One"; 
0
foreach($mainarray as $array){ //check for each 
    if($array['required']==1) 
    //doSome Task 
    if($array['required']==0) 
0

這可能是最hackish的解決方案,我會拿出全部今早,但如果你在拍攝了一個快速班輪,這應該這樣做:

if(in_array(1, array_column($myArray, 'required'))) 
    echo 'required'; 

編輯:這是假設你有PHP 5.5+手頭

更新:在t仔細一看後他的in_array()文檔,這應該是可能的:

if(in_array(array('required' => 1), $myArray)) 
    echo 'required'; 
0

作爲替代,獨立的陣列結構的我的方式(假設$ X握着你的數據結構):

if (preg_match('/"required":[01]/',json_encode($x))) { 
    // The array structure contains "required" => 0 or "required" => 1 
} else { 
    // Negative 
}