27

Say I have an array like this:

$array = array('', '', 'other', '', 'other');

How can I count the number with a given value (in the example blank)?

And do it efficiently? (for about a dozen arrays with hundreds of elements each). This example times out (over 30 seconds):

function without($array) {
    $counter = 0;
    for($i = 0, $e = count($array); $i < $e; $i++) {
        if(empty($array[$i])) {
            $counter += 1;
        }
    }
    return $counter;
}

In this case, the number of blank elements is 3.

1
  • Cellfish: array_count_values works, but the script which normally takes 1 - 2sec to execute took nearly 20sec! Johnathan: array_keys did not work for this particular function. Steve: array_reduce also works but not as fast as I would like. Camomile: Simply by changing $value === '' to empty($value) that turned out to be the fastest way to do this. function without($array) { $count = 0; foreach($array as $value) { if(empty($value)) { $count++; } } return $count; } Commented Aug 23, 2009 at 20:19

8 Answers 8

37

Use array_count _values to get an array with everything counted for you.

Sign up to request clarification or add additional context in comments.

1 Comment

array_count_values() is not reliable if the count is zero because only found values become keys
32

Here is just an idea. You could use array_keys($myArray, "") using the optional second parameter which specifies a search-value. Then count the result.

$myArray = array("", "", "other", "", "other");
$length  = count(array_keys($myArray, ""));

2 Comments

This is a great alternative, especially if dealing with boolean values.
@SteveTauber or arrays as the search value :)
6

I don’t know if this would be faster, but it's something to try:

$counter = 0;
foreach($array as $value)
{
  if($value === '')
    $counter++;
}
echo $counter;

Comments

3

You could also try array_reduce, with a function which would just count the value you are interested in. For example,

function is_empty( $v, $w )
{ return empty( $w ) ? ($v + 1) : $v; }

array_reduce( $array, 'is_empty', 0 );

Some benchmarking might tell you if this is faster than array_count_values().

Comments

2

We use the array_filter function to find out the number of values in an array:

$array = array('', '', 'other', '', 'other');
$filled_array = array_filter($array); // Will return only the filled values
$count = count($filled_array);
echo $count; // Returns array count

Comments

1

The following is generally for counting blanks only. It really depends on the use case and speed needed. Personally, I like doing things on one line.

It is like Cellfish's answer, though. But you still need a line to extract the data needed though to another variable.

$r = count($x) - count(array_filter($x));

1 Comment

Or use callback: count(array_filter($x, function ($v) {return '' === $v;}))
-4
function arrayvaluecount($array) {

    $counter = 0;
    foreach($array as $val) {

        list($v) = $val;
        if($v) {
            $counter = $counter + 1;
        }
    }
    return $counter;
}

Comments

-4
function countarray($array)
{
    $count = count($array);
    return $count;
}

$test = $array = array('', '', 'other', '', 'other');
echo countarray($test);

1 Comment

$array=array('','','other','','other'); $filled_array=array_filter($array);// will return only filled values $count=count($filled_array); echo $count;// returns array count

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.