views:

57

answers:

3

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?)

+1  A: 

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.

Peter Lindqvist
+2  A: 

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

Roland Bouman
A: 

$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

MindStalker