32

How do I find if an array has one or more elements?

I need to execute a block of code where the size of the array is greater than zero.

if ($result > 0) {
    // Here is the code body which I want to execute
} 
else {
    // Here is some other code
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Sajid Mehmood
  • 483
  • 2
  • 6
  • 18

8 Answers8

40

You can use the count() or sizeof() PHP functions:

if (sizeof($result) > 0) {
    echo "array size is greater than zero";
}
else {
    echo "array size is zero";
}

Or you can use:

if (count($result) > 0) {
    echo "array size is greater than zero";
}
else {
    echo "array size is zero";
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dani
  • 905
  • 7
  • 14
  • 1
    PHP 7 Throws a warning when using sizeof or count in an array. Is there a better way now? – Christopher Smit Jun 06 '18 at 12:47
  • 2
    The warning is thrown only if you pass an invalid type to `count`. See changelog here: http://php.net/manual/en/function.count.php. – Amade Sep 04 '18 at 21:10
14

count — Count all elements in an array, or something in an object

int count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )

Counts all elements in an array, or something in an object.

Example:

<?php
    $a[0] = 1;
    $a[1] = 3;
    $a[2] = 5;
    $result = count($a);
    // $result == 3

In your case, it is like:

if (count($array) > 0)
{
    // Execute some block of code here
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
  • If we count above one time then can we again count it in the if condition ? – Sajid Mehmood Feb 14 '17 at 13:03
  • You can count as many times as you please, but sometimes it's not necessary. Try not to do unnecessary things, that will just stain your code. – Zeke Feb 14 '17 at 13:24
5

If you want to only check if the array is not empty, you should use empty() - it is much faster than count(), and it is also more readable:

if (!empty($result)) {
    // ...
} else {
    // ...
}
rob006
  • 21,383
  • 5
  • 53
  • 74
3

You could avoid length retrieve and check using a simple foreach:

foreach($result as $key=>$value) {
    echo $value;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
1

@Sajid Mehmood in PHP we have count() to count the length of an array, when count() returns 0 that means that array is empty

Let’s take an example for your understanding:

<?php
    $arr1 = array(1); // With one value which will give 1 count
    $arr2 = array();  // With no value which will give 0 count

    // Now I want that the array which has greater than 0 count should print other wise not so

    if (count($arr1)) {
        print_r($arr1);
    }
    else {
        echo "Sorry, array1 has 0 count";
    }

    if (count($arr2)) {
        print_r($arr2);
    }
    else {
        echo "Sorry, array2 has 0 count";
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lazyCoder
  • 2,544
  • 3
  • 22
  • 41
1

Pro Tip:

If you are sure that:

  1. the variable exists (isset) AND
  2. the variable type is an array (is_array) ...might be true of all is_iterables, but I haven't researched that extension of the question scope.

Then you don't need to call any functions. An array with one or more elements has a boolean value of true. An array with no elements has a boolean value of false.

Code: (Demo)

var_export((bool)[]);
echo "\n";
var_export((bool)['not empty']);
echo "\n";
var_export((bool)[0]);
echo "\n";
var_export((bool)[null]);
echo "\n";
var_export((bool)[false]);
echo "\n";

$noElements = [];
if ($noElements) {
    echo 'not empty';
} else {
    echo 'empty';
}

Output:

false
true
true
true
true
empty    
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

For those who start with the array in PHP presented it this way: more information here

//Array
$result = array(1,2,3,4);

//Count all the elements of an array or something of an object
if (count($result) > 0) {
    print_r($result);
} 

// Or 
// Determines if a variable is empty
if (!empty($result)) {
    print_r($result);
}

// Or 
// sizeof - Alias of count ()
if (sizeof($result)) {
    print_r($result);
} 
shades3002
  • 879
  • 9
  • 11
-1
<pre>
$ii = 1;
$arry_count = count($args);
foreach ( $args as $post)
{
    if( $ii == $arry_count )
    {
        $last = 'blog_last_item';
    }
    echo $last;
    $ii++; 
}
</pre>
AS Mackay
  • 2,831
  • 9
  • 19
  • 25
  • Can you add some explanation to your code? What does `$last` do? – Nico Haase Nov 09 '18 at 12:25
  • Welcome to Stack Overflow! Thank you for the code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its [long-term value](https://meta.stackexchange.com/q/114762/206345) by describing why this is a good solution to the problem, and would make it more useful to future readers with other similar questions. Please edit your answer to add some explanation, including the assumptions you've made. – sepehr Nov 09 '18 at 16:09