davidannis Posted June 15, 2014 Share Posted June 15, 2014 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. Link to comment https://forums.phpfreaks.com/topic/289165-array_filter-and-count-not-acting-as-expected/ Share on other sites More sharing options...
mac_gyver Posted June 15, 2014 Share Posted June 15, 2014 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)) Link to comment https://forums.phpfreaks.com/topic/289165-array_filter-and-count-not-acting-as-expected/#findComment-1482698 Share on other sites More sharing options...
requinix Posted June 15, 2014 Share Posted June 15, 2014 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. Link to comment https://forums.phpfreaks.com/topic/289165-array_filter-and-count-not-acting-as-expected/#findComment-1482703 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.