Bert1455 Posted July 28, 2013 Share Posted July 28, 2013 I've been trying to learn PHP with the PHP Solutions book second editions by David Powers. I seemingly can't find anyway of contacting him about the code I found in the book. <?phpforeach ($_POST as $key => $value) { // assign to temporary variable and strip whitespace if not an array $temp = is_array($value) ? $value : trim($value);// if empty and required, add to $missing arrayif (empty($temp) && in_array($key, $required)) { $missing[] = $key;} elseif (in_array($key, $expected)) { // otherwise, assign to a variable of the same name as $key ${$key} = $temp; } } I understand that is_array checks whether or not $value is an array but I can't find a scenario in which it would be array and therefore why it would be there at all. Also I the last part relies on the fact above which I don't understand as well. Quote Link to comment https://forums.phpfreaks.com/topic/280606-is_array-php-solutions-helppppp/ Share on other sites More sharing options...
mac_gyver Posted July 28, 2013 Share Posted July 28, 2013 a form field can be an array name, name='some_name[]'. this allows you to have multiple related fields, typically checkboxed/radiobuttons, that share a common name and can be iterated over as an array after they have been submitted. Quote Link to comment https://forums.phpfreaks.com/topic/280606-is_array-php-solutions-helppppp/#findComment-1442525 Share on other sites More sharing options...
Bert1455 Posted July 28, 2013 Author Share Posted July 28, 2013 Wait so $value is a form field? I really don't understand this portion of his code. I thought $value was user input. And I was thinking why user input would ever be an array? I thought $key would be the form fields. I'm sorry I'm a beginner and this made me quit confused. Quote Link to comment https://forums.phpfreaks.com/topic/280606-is_array-php-solutions-helppppp/#findComment-1442529 Share on other sites More sharing options...
AbraCadaver Posted July 29, 2013 Share Posted July 29, 2013 (edited) Form fields are user input. If you submit the following checkboxes named: cb[0], cb[1], cb[2] with each one having a value of 'yes', and all are checked, then when submitted you have a $_POST['cb'] array. So the $key is 'cb' and the $value is an array with three items. The print_r() of the $_POST array would look like: Array ( [cb] => Array ( [0] => yes [1] => yes [2] => yes ) ) Edited July 29, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/280606-is_array-php-solutions-helppppp/#findComment-1442558 Share on other sites More sharing options...
Bert1455 Posted July 29, 2013 Author Share Posted July 29, 2013 Thank you Cadaver. My only question now is how this would apply to a form in which users type in say their e-mail address or leaving comments? Quote Link to comment https://forums.phpfreaks.com/topic/280606-is_array-php-solutions-helppppp/#findComment-1442616 Share on other sites More sharing options...
jcbones Posted July 29, 2013 Share Posted July 29, 2013 <?php foreach ($_POST as $key => $value) { //for each of the user inputs (form fields). // assign to temporary variable and strip whitespace if not an array $temp = is_array($value) ? $value : trim($value); //if the value is an array, assign it to $temp, otherwise trim all the whitespace from the value, then assign it to $temp. // if empty and required, add to $missing array if (empty($temp) && in_array($key, $required)) { //if $temp is empty AND the current $key is not in the $required array. $missing[] = $key; //assign the current $key to an array named $missing. } elseif (in_array($key, $expected)) { //If non of the above are true, but the current $key is in the $expected array. // otherwise, assign to a variable of the same name as $key ${$key} = $temp; } //assign the $temp variable to a variable of the same name as the current $key (variable variable), which is the same name as your form field name. } //any data passed that isn't in the $required array, nor in the $expected array WILL BE discarded. I tried some clarification. Quote Link to comment https://forums.phpfreaks.com/topic/280606-is_array-php-solutions-helppppp/#findComment-1442619 Share on other sites More sharing options...
Bert1455 Posted July 30, 2013 Author Share Posted July 30, 2013 The context is that value would be user input in a form. I'm confused about the is_array portion in the 4th line. The user input shown in the book states would be something like an e-mail, name, or comment. I'm confused as to why is_array would be used. I mean when will user input every be an array? Wouldn't that make the condition always true? Anyways, thanks for the reply, I finally understood that last portion at the bottom, which I thought was assigning $key to the $temp variable defined above. Quote Link to comment https://forums.phpfreaks.com/topic/280606-is_array-php-solutions-helppppp/#findComment-1442631 Share on other sites More sharing options...
KevinM1 Posted July 30, 2013 Share Posted July 30, 2013 A collection of submitted checkboxes could be an array. Quote Link to comment https://forums.phpfreaks.com/topic/280606-is_array-php-solutions-helppppp/#findComment-1442633 Share on other sites More sharing options...
Bert1455 Posted July 30, 2013 Author Share Posted July 30, 2013 I'm sorry if I didn't make it clear and that's also what I thought initially, it's in the PHP Solution 2nd Editions ( I know quite few people who have it), but the book states clearly that it's user text input. Quote Link to comment https://forums.phpfreaks.com/topic/280606-is_array-php-solutions-helppppp/#findComment-1442634 Share on other sites More sharing options...
mac_gyver Posted July 30, 2013 Share Posted July 30, 2013 that line of code is using a Ternary Operator - a short form of an if(){}else{} statement, in case (conditionally) the form field is an array. the code is not saying the form field must be an array, but if it is an array, the array is assigned to $temp. if it is not an array, the trimmed value is assigned to $temp. that line is the same as - if(is_array($value)){ // $value is an array, assign it directly $temp (since trying to use trim() on it won't do anything). the code should actually be part of a recursive function so that if the data is an array at this point, the function is called again to process the next level of the array $temp = $value; } else { // $value is not an array, it is a scaler variable, assign the trimmed $value to $temp $temp = trim($value); } Quote Link to comment https://forums.phpfreaks.com/topic/280606-is_array-php-solutions-helppppp/#findComment-1442656 Share on other sites More sharing options...
KevinM1 Posted July 30, 2013 Share Posted July 30, 2013 Exactly. Would it make more sense if it looked like: foreach ($_POST as $key => $value) { if (is_array($value)) { $temp = $value; } else { $temp = trim($value); } } ? Because that's what the ternary operator is actually doing: (condition) ? if true : if false So, in this case, you're looping through all of $_POST blindly, and can't tell from the outset if you're dealing with a single entry or a collection of them, which is what that ternary operator checks for. Quote Link to comment https://forums.phpfreaks.com/topic/280606-is_array-php-solutions-helppppp/#findComment-1442705 Share on other sites More sharing options...
Solution .josh Posted July 30, 2013 Solution Share Posted July 30, 2013 A form can have any number and type of elements(text fields, radio buttons, checkboxes, hidden fields, dropdowns, etc.). $_POST is an array of all the form elements and their values that was submitted. Depending on how complex the form is, $_POST can be anything from an array with a single element, to a multi-dimensional array. For example, with a simple form like this: <?php echo "<pre>";print_r($_POST);echo "</pre>"; ?> <form action='' method='post'> <input type='text' name='email' /> <input type='submit' name='submit' value='submit' /> </form> $_POST will look like this: Array ( [email] => foo@bar.com [submit] => submit ) But with a more complex form, like one with checkboxes: <?php echo "<pre>";print_r($_POST);echo "</pre>"; ?> <form action='' method='post'> <input type='text' name='email' /> <br/> <input type="checkbox" name="animals[]" value="dog" />dog <br/> <input type="checkbox" name="animals[]" value="cat" />cat <br/> <input type="checkbox" name="animals[]" value="mouse" />mouse <br/> <input type='submit' name='submit' value='submit' /> </form> $_POST will look like this: Array ( [email] => foo@bar.com [animals] => Array ( [0] => dog [1] => mouse ) [submit] => submit ) So from this, is_array($_POST['email']) would be false, since that is just a string "foo@bar.com". But is_array($_POST['animals']) would be true. Checking whether or not the posted value is an array may or may not be relevant to the actual code example in the book; you'd have to post the form code from the book for us to determine that. Quote Link to comment https://forums.phpfreaks.com/topic/280606-is_array-php-solutions-helppppp/#findComment-1442715 Share on other sites More sharing options...
Bert1455 Posted July 30, 2013 Author Share Posted July 30, 2013 Thanks guys, I really appreciate it and this website is extremely helpful. Quote Link to comment https://forums.phpfreaks.com/topic/280606-is_array-php-solutions-helppppp/#findComment-1442730 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.