繁体   English   中英

我如何使用PHP中的数组数来制作if语句?

[英]How can I make if statement using the number of array in PHP?

findParent()函数查找数组。 可能有一个数组或多个数组。

现在我要使if语句取决于数组的数量。

我怎样才能使if语句使用数组数呢?

function findParent($order_id){

  ...
  $Q = $this->db->get('omc_order_item');
        if ($Q->num_rows() > 0){
            foreach ($Q->result_array() as $row){
                $data[] = $row;
            }
        }
   ... 
   return $data; 

 }

我试过了,但是没有用。

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

它必须检查$ childless是否小于2。

我如何更改它,以便它将检查数组的数量为1(可以小于2,不是吗?)

我相信您正在寻找的是count()函数。 您向它传递一个数组,它返回数组中元素的数量。 请参阅: http//php.net/manual/en/function.count.php

如果你的意思是数组数

if (count($childless) < 2)

从您的示例看来, findParent()函数返回一个数组数组。 要与结果数组中包含的数组数量进行比较,可以使用count(array())函数。

它返回作为参数传递的数组中的数组项数。

例如

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

将输出2

有关文档,请参见php.net/count页面。

$ childless拥有很多信息,而不仅仅是行数,您需要从$ childless中提取行数。 if (count($childless) < 2 )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM