Jump to content

is_array PHP Solutions HELPPPPP!


Bert1455
Go to solution Solved by .josh,

Recommended Posts

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.

 

 

<?php
foreach ($_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 array
if (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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by AbraCadaver
Link to comment
Share on other sites

 

<?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.

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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);
}
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

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.