2012-11-29 16 views
1

我想檢查數據數組中是否存在數據,以便我可以執行函數。我會用in_array嗎?如果數組存在數據,檢查數據

陣:

["OpenHomes"]=> 
array(2) { 
    [0]=> 
    array(2) { 
    ["Start"]=> 
    string(21) "/Date(1354323600000)/" 
    ["End"]=> 
    string(21) "/Date(1354326300000)/" 
    } 

會只是我下面?

if(in_array($detail['OpenHomes'])) 
{ 
    echo 'yes'; 
}else{ 
    echo 'no'; 
} 

我想檢查在OpenHomes內是否有任何東西存在。

回答

2

試試這個

if(!empty($detail) && in_array('OpenHomes',$detail)) 
{ 
    echo 'yes'; 
}else{ 
     echo 'no'; 
} 
1

你也可以檢查

if ($detail['OpenHomes']) { 

if (count($detail['OpenHomes']) > 0) { ... } 
1

您還可以使用:

count($detail['OpenHomes']) 

sizeof($detail['OpenHomes']) // its alias, same function with different name 

獲取該數組中元素的數量,但也會計數空元素。

1

或者這

if(!empty($details['openHomes'])) 
{ 
    // 
} 
else 
{ 
    // 
} 
相關問題