如何在数组php中只获取非空元素计数

时间:2022-12-08 03:34:55

i want to get only not null values count in that array , if i use count() or sizeof it will get the null indexes also .

我想在数组中只获取非空值计数,如果我使用count()或者sizeof它也会得到空索引。

in my case

在我的例子中

i have an array like this Array ( [0] => )

我有一个这样的数组([0]=>)

the count is 1 . but i want to get the not null count , inthis case it should be 0 , how can i do this , please help............................

计数是1。但我想要得到非空数,在这种情况下应该是0,我该怎么做呢,请帮忙做一下。

5 个解决方案

#1


8  

simply use array_filter() without callback

只需使用array_filter()而不回调

print_r(array_filter($entry));

#2


9  

$count = count(array_filter($array));

array_filter will remove any entries that evaluate to false, such as null, the number 0 and empty strings. If you want only null to be removed, you need:

array_filter将删除任何计算为false的条目,如null、数字0和空字符串。如果您希望仅删除null,则需要:

$count = count(array_filter($array,create_function('$a','return $a !== null;')));

#3


1  

something like...

之类的……

$count=0;
foreach ($array as $k => $v)
{
    if (!empty($v))
    {
        $count++;
    }
}

should do the trick. you could also wrap it in a function like:

应该足够了。您也可以将其封装在如下函数中:

function countArray($array)
{
$count=0;
foreach ($array as $k => $v)
{
    if (!empty($v))
    {
        $count++;
    }
}
return $count;

}

echo countArray($array);

#4


0  

although i'm not that good with PHP i think you should programatically remove empty elements from the array the link might help

虽然我不太擅长PHP,但我认为您应该编程地从数组中删除空元素,这个链接可能会有所帮助。

#5


0  

One option is

一种选择是

echo "Count is ".count(array_filter($array_with_nulls, 'strlen'));

If you don't count empty and nulls values you can do this

如果不计算空值和空值,可以这样做

echo "Count is ".count(array_filter($array_with_nulls));

In this blog you can see a little more info

在这个博客中你可以看到更多的信息

http://briancray.com/2009/04/25/remove-null-values-php-arrays/

http://briancray.com/2009/04/25/remove-null-values-php-arrays/

#1


8  

simply use array_filter() without callback

只需使用array_filter()而不回调

print_r(array_filter($entry));

#2


9  

$count = count(array_filter($array));

array_filter will remove any entries that evaluate to false, such as null, the number 0 and empty strings. If you want only null to be removed, you need:

array_filter将删除任何计算为false的条目,如null、数字0和空字符串。如果您希望仅删除null,则需要:

$count = count(array_filter($array,create_function('$a','return $a !== null;')));

#3


1  

something like...

之类的……

$count=0;
foreach ($array as $k => $v)
{
    if (!empty($v))
    {
        $count++;
    }
}

should do the trick. you could also wrap it in a function like:

应该足够了。您也可以将其封装在如下函数中:

function countArray($array)
{
$count=0;
foreach ($array as $k => $v)
{
    if (!empty($v))
    {
        $count++;
    }
}
return $count;

}

echo countArray($array);

#4


0  

although i'm not that good with PHP i think you should programatically remove empty elements from the array the link might help

虽然我不太擅长PHP,但我认为您应该编程地从数组中删除空元素,这个链接可能会有所帮助。

#5


0  

One option is

一种选择是

echo "Count is ".count(array_filter($array_with_nulls, 'strlen'));

If you don't count empty and nulls values you can do this

如果不计算空值和空值,可以这样做

echo "Count is ".count(array_filter($array_with_nulls));

In this blog you can see a little more info

在这个博客中你可以看到更多的信息

http://briancray.com/2009/04/25/remove-null-values-php-arrays/

http://briancray.com/2009/04/25/remove-null-values-php-arrays/