Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if an array has any elements or not?

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
}
like image 226
Sajid Mehmood Avatar asked Feb 14 '17 12:02

Sajid Mehmood


People also ask

How do you check if an array has no elements?

To check if an array is empty or not, you can use the . length property. The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not.

How do you check the array is empty or not?

The array can be checked if it is empty by using the array. length property. This property returns the number of elements in the array. If the number is greater than 0, it evaluates to true.

How do you check if an array contains any element of another array?

Use the inbuilt ES6 function some() to iterate through each and every element of first array and to test the array. Use the inbuilt function includes() with second array to check if element exist in the first array or not. If element exist then return true else return false.

How do I check if an array contains an array?

To check if the array contains an array in Javascript, use array some(), and array includes() function. The array some() method checks each element against a test method and returns true if any array item passes the test function.


6 Answers

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";
}
like image 75
Dani Avatar answered Oct 11 '22 02:10

Dani


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
}
like image 23
Mayank Pandeyz Avatar answered Oct 11 '22 01:10

Mayank Pandeyz


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

foreach($result as $key=>$value) {
    echo $value;
}
like image 42
ScaisEdge Avatar answered Oct 11 '22 03:10

ScaisEdge


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 {
    // ...
}
like image 44
rob006 Avatar answered Oct 11 '22 02:10

rob006


@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";
    }
like image 23
Vishal Solanki Avatar answered Oct 11 '22 01:10

Vishal Solanki


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    
like image 37
mickmackusa Avatar answered Oct 11 '22 01:10

mickmackusa