2010-01-06 107 views
1

findParent()函數查找數組。可能有一個數組或多個數組。如何使用PHP中的數組數來創建if語句?

現在我想讓if語句取決於數組的個數。

我該如何使用數組的數量來製作if語句?

​​

我試過這個,但它不起作用。

function deleteitem($order_id){ 
    $childless = $this->MOrders->findParent($order_id); 
    if ($childless<2){ 
     $data['childlessorder']= $this->MOrders->findParent($order_id); 
... 

必須檢查,如果沒有子女$小於2

我如何改變它,這樣它會檢查陣列的數量爲1(可小於2的,不是嗎?)

回答

1

如果你的意思,然後

if (count($childless) < 2) 

陣列的數量從您的例子,它看起來像findParent()函數返回一個數組的數組。要比較數組中包含的數組,您可以使用count(array())函數。

它返回作爲參數傳遞的數組中的數組項數。

例如

echo count(
    array(
     0 => array(1,2), 
     1 => array(3,4) 
    ) 
); 

將輸出2

對於文檔看到php.net/count頁。

0

$ childless包含很多信息,而不僅僅是行數,您需要從$ childless中提取行數。例如if (count($childless) < 2)