Jump to content

array_filter and count not acting as expected


davidannis

Recommended Posts

I have a form on a page that has checkboxes for a number of entities, including one that is new. I want to determine how many checkboxes were checked for the new entity.  The  html is:

<div id="new" style="display:block;">Which method(s) of valuation would you like to use for this report?<br />
<input type="checkbox" name="method_code['new'][]" value="EE">Excess Earnings<br />
<input type="checkbox" name="method_code['new'][]" value="DCF">Discounted Cash Flow</div>
<div id="75" style="display:none;">Which method(s) of valuation would you like to use for this report?<br /><input type="checkbox" name="method_code['75'][]" value="EE" checked="checked" >Excess Earnings<br />
<input type="checkbox" name="method_code['75'][]" value="DCF" checked="checked" >Discounted Cash Flow</div><div id="79" style="display:none;">Which method(s) of valuation would you like to use for this report?<br />
<input type="checkbox" name="method_code['79'][]" value="EE">Excess Earnings<br /><input type="checkbox" name="method_code['79'][]" value="DCF">Discounted Cash Flow</div>
<div id="77" style="display:none;">Which method(s) of valuation would you like to use for this report?<br />
<input type="checkbox" name="method_code['77'][]" value="EE">Excess Earnings<br />
<input type="checkbox" name="method_code['77'][]" value="DCF">Discounted Cash Flow</div>

I looked for a method of counting the nuber of checked boxes for a given entity and found

So, to count only non-empty:

count(array_filter($array));

here and here

 

So I implemented that in my code and it doesn't work. I put in the following to debug:

    echo '<pre>';
    print_r($_POST['method_code']);
    echo '</pre>';
    echo "arraycount ".count(array_filter($_POST['method_code']['new']));die();

and I get the following output:

Array
(
    ['new'] => Array
        (
            [0] => EE
        )

    ['75'] => Array
        (
            [0] => EE
            [1] => DCF
        )

    ['78'] => Array
        (
            [0] => EE
        )

)

arraycount 0

Why is the count 0? I expect that count(array_filter($_POST['method_code']['new'])) would be one since there is one element in that array.

in your form field names, because you have single-quotes around the new, 75, 79, ... index names, the names will also include those single-quotes. remove the single quotes.
 
your print_r() should show the following (with no quotes around the index names) -
Array
(
[new] => Array
(
[0] => EE
)

[75] => Array
(
[0] => EE
[1] => DCF
)

[78] => Array
(
[0] => EE
)

)

By the way, you don't need array_filter(). I think that's intended to filter out the checkboxes that weren't checked? In HTML only the checkboxes that were checked will be submitted. As that print_r() output shows.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.