Jump to content

array_filter and count not acting as expected


Go to solution Solved by mac_gyver,

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.

  • Solution

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
)

)

Edited by mac_gyver

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.