shin
shin

Reputation: 32721

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

findParent() function find array. There could be one array or more than one.

Now I want to make if statement depends on the number of array.

How can I make if statement using the number of array?

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; 

 }

I tried this, but it does not work.

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

It must be checking if $childless is less than 2.

How can I change it so that it will check the number of array is 1 (can be less than 2, doesn't it?)

Upvotes: 1

Views: 311

Answers (3)

Peter Lindqvist
Peter Lindqvist

Reputation: 10200

If you mean number of arrays then

if (count($childless) < 2)

From your example it looks like the findParent() function returns an array of arrays. To compare against the number of arrays contained within the resulting array you would use the count(array()) function.

It returns the number of array items within the array passed as its argument.

For instance

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

would output 2

For documentation see the php.net/count page.

Upvotes: 1

MindStalker
MindStalker

Reputation: 14864

$childless holds a lot of information, not just the number of rows, you need to extract the row count from $childless. if (count($childless) < 2 ) for example

Upvotes: 0

Roland Bouman
Roland Bouman

Reputation: 31961

I believe what you are looking for is the count() function. You pass it an array, and it returns the number of elements in the array. See: http://php.net/manual/en/function.count.php

Upvotes: 2

Related Questions