Showing posts with label PHP sizeof. Show all posts
Showing posts with label PHP sizeof. Show all posts

Friday, 2 August 2019

How to check for empty values within a array using PHP

If you have an array called $array with the below values
Array
(
    [0] => 1299016800
    [1] => 
    [2] => 
)
As we can see in the above example we have 2 empty values within the array, now how do we determine this using PHP
First we get the size of the array
$mainSize = sizeof($array);
Now we get the size of the array without the empty values
$emptySize =sizeof(array_filter($array));
Now we compare the 2 different sizes, if they are equal to each other, then it means that there aren’t empty values within the array, else there are empty values within the array
$mainSize = sizeof($array);
$emptySize =sizeof(array_filter($array));
 
if($mainSize == $emptySize){
              echo 'Array not empty';
}else{
              echo 'Array is empty';
}

Thursday, 25 September 2014

sizeof in PHP

PHP sizeof() function is used to count the elements from an array, or the properties of an object.
It is an alias to count() function.

Syntax:

sizeof(array,mode)
 
Parameters Description: 
array : Required. Determines the array or object count.
mode : Optional. Determines the mode of the function. Conceivable values:
  • 0 -Default. Does not detect multidimensional arrays (arrays within arrays)
  • 1 -Detects multidimensional arrays
Note : This parameter was added in PHP 4.2
Note : PHP sizeof() function may return 0 if a variable isn't set, but it may also return 0 if a variable contains an empty array.
           The isset() function can be used to test if a variable is set.

Example:

<?php
$daysarr = array( "Sunday","Monday","Tuesday","Wednesday","Thursday" );
$result= sizeof($daysarr);
echo $result;
?>

Output will be:

5