chop Posted February 1, 2012 Share Posted February 1, 2012 As in Larry's PHP book PHP 6 and MySQL 5, I have been using: $trimmed = array_map('trim', $_POST); to put all the $_POST[] data into an array. But there's a problem now that I'm using the checkbox input field as set to input an array like this: <label> <input type="checkbox" name="media[]" value="acrylic" /> acrylic </label> The array_map doesn't handle $_POST[] arrays I could make each of the checkbox inputs with unique names like so: <input type="checkbox" name="acrylic" value="acrylic" /> But an array seems in order here. What should I do?? Link to comment Share on other sites More sharing options...
chop Posted February 1, 2012 Author Share Posted February 1, 2012 Part 2 of this question is: I get the error "Warning: trim() expects parameter 1 to be string, array given in /ApacheServer/ApacheDocRoot/phpPNEdevelopment/Gill/maaRegistrationForm.php on line 124" ... but it doesn't cause any problems in the running of the script, it will continue. Is it proper to ignore the error and then handle the $_POST[media] array in whatever way I want? Or should no script be considered "good code" as long as there is an error "at large" no matter if it is benign? Link to comment Share on other sites More sharing options...
rob Posted February 1, 2012 Share Posted February 1, 2012 That's a warning (non-fatal error) therefore script execution isn't stopped, personally I wouldn't ignore it. Take a look at array_walk_recursive() on the php website. Link to comment Share on other sites More sharing options...
chop Posted February 1, 2012 Author Share Posted February 1, 2012 I'm not sure that I want a recursive procedure to be applied to $trimmed array (though I'm not sure). I just want to get the "$trimmed = array_map('trim', $_POST);" to ignore the array variable media[] that is defined in the checkbox input field: <input type="checkbox" name="media[]" value="acrylic" /> I'll deal with it myself in the $_POST['media'] array form. Link to comment Share on other sites More sharing options...
rob Posted February 1, 2012 Share Posted February 1, 2012 You would use array_walk_recursive() - instead of array_map() - on $_POST, that way trimming all your form submitted values. 1 Link to comment Share on other sites More sharing options...
chop Posted February 1, 2012 Author Share Posted February 1, 2012 When I do this: $trimmed= array_walk_recursive($_POST); $trimmed = array_map('trim', $trimmed);// thought I'd need this to do the actual trimming or this: $trimmed= array_walk_recursive($_POST); The fields don't validate-- Link to comment Share on other sites More sharing options...
Larry Posted February 1, 2012 Share Posted February 1, 2012 Did you check out the PHP manual for how to use array_walk_recursive()? Link to comment Share on other sites More sharing options...
chop Posted February 1, 2012 Author Share Posted February 1, 2012 I had looked at it before but didn't get how the array items would get trimmed as in "array_map('trim', $_POST)". So I tried to integrate "array_map" into the "array_walk_recursive()" formula and overcomplicated things. However, I discovered that the array items do get trimmed anyway -though I don't know how unless it's just part of array_walk_recursive().?? So, thanks for bearing with me. I am no longer getting the "Warning: trim() expects parameter 1 to be string,..." which was my main concern. Link to comment Share on other sites More sharing options...
Recommended Posts