1

I have a form that post this values:

actusr 16

crediti[0] CD000001125 crediti[1] CD000001126 crediti[2] CD000001127

garanzia[0] 11556493.48 garanzia[1] garanzia[2]

soff_for SF000000179

soff_who SF000000002

tipo_gar[0] 1 tipo_gar[1] tipo_gar[2]

I'd like to count how many items in tipo_gar array have a non empty value. I can do it via foreach:

$count = 0;
foreach($_POST['tipo_gar'] as $to_count){
    if($to_count != ''){
        $count=$count+1;
    }
}

but I guess there is a built-in php functionality I am not aware of that will do the trick saving me some lines of code.

1

1 Answer 1

2

You can count non-empty value using array_filter(). array_filter can only keep the values that are non-empty in the array.

count(array_filter($_POST['tipo_gar']));
Sign up to request clarification or add additional context in comments.

2 Comments

ok, so I build an array with only non empty items and then count it. Smart! Thanks for the hint!
will be back when I can accept your answer :)