H H-O-V Established Member ★ 20 ★ Impact 2 Mar 12, 2007 757 views 2 replies #1 I have an array of values from a form ($_POST) and they are all checkboxes so carrots => on apples => etc is there any way of selecting just the on ones in another array?
I have an array of values from a form ($_POST) and they are all checkboxes so carrots => on apples => etc is there any way of selecting just the on ones in another array?
Dan Buy my domains.VIP Member VIP ★ 15 ★ Impact 108 Mar 12, 2007 #2 How do you want them to be selected? If you are going to loop through and do something to each one anyways, you don't really need any extra code. PHP: foreach ($_POST as $key => $value) { if ($value == "on") { // do something. } }
How do you want them to be selected? If you are going to loop through and do something to each one anyways, you don't really need any extra code. PHP: foreach ($_POST as $key => $value) { if ($value == "on") { // do something. } }
Peter VIP Member VIP ★ 20 ★ Impact 209 Mar 12, 2007 #3 Dan's solution will work 100% although another way of doing it would be like:- PHP: <?php $_POST = array('banana'=>'on','orange'=>'on','grapefruit'=>'off','lettuce'=>'on'); function check_on($setting, $fruit) { if ($setting == 'on') { echo $fruit.' is on</br>'; } } array_walk($_POST, 'check_on'); ?>
Dan's solution will work 100% although another way of doing it would be like:- PHP: <?php $_POST = array('banana'=>'on','orange'=>'on','grapefruit'=>'off','lettuce'=>'on'); function check_on($setting, $fruit) { if ($setting == 'on') { echo $fruit.' is on</br>'; } } array_walk($_POST, 'check_on'); ?>