SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need some help with array problem

    Hello,

    I have the following code in my script:

    Code:
    answer 1 <INPUT TYPE="text" NAME="answer[]" VALUE="" SIZE="30" MAXLENGTH="100">
    answer 2 <INPUT TYPE="text" NAME="answer[]" VALUE="" SIZE="30" MAXLENGTH="100">
    answer 3 <INPUT TYPE="text" NAME="answer[]" VALUE="" SIZE="30" MAXLENGTH="100">
    answer 4 <INPUT TYPE="text" NAME="answer[]" VALUE="" SIZE="30" MAXLENGTH="100">
    answer 5 <INPUT TYPE="text" NAME="answer[]" VALUE="" SIZE="30" MAXLENGTH="100">
    ......
    As you can see, I'm trying to get all the aswers into an array. But, when someone leave a field unanswered, then the array will contain a "". I'm trying to filter out all of the "", and strip all slaches as well.

    I'm using the following code to do this:

    Code:
    foreach($answer as $key => $val)
       { 
       if ($val == '') {array_pop($answer);} 
       else {$val = stripslashes($val);}
       }
    But this code does not work! Any one know why?

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    array_pop() performs a very specific task and cannot be used in the way you have tried to

    Arrays in PHP are a multi-talented animal. They are not just arrays in the strict sense of what an array is in C, C++ or Java for that matter. One way in which a PHP array can be used is as a stack. A stack is a collection (a.k.a data structure) that behaves in a very specific manner - you add elements to the top of the stack (known as pushing onto the stack) and remove elements from the top of the stack (known as popping elements). Think of a stack as a pile of dinner plates. You can only push a dinner plate onto the stack by placing it at the top of the stack. When you want to remove a dinner plate from the stack your only choice is to pop a plate from the top. Thus the first plate inserted into the stack will always be the last one popped out. This behaviour is fundamental to how computer programs work at a very low level (beyond my feable knowledge).

    Anyway, that doesn't directly answer your question The only way I can think of doing what you want is to loop through the array as you do using foreach and copy the value accross to a new array IF it meets your conditions
    PHP Code:
    foreach($answer as $anAnswer) {
       if ( 
    $anAnswer != '' ) {
          
    $AnswerCopy[] = $anAnswer;
       }


  3. #3
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm quite surprised PHP doesn't have an array function for removing the specified elemtn (or all occurences of it) from an array - it's got array functions for virtually everything else

    http://www.php.net/manual/en/ref.array.php

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My mistake DeNasio, if you want to preserve the array for later use after taking out the blank elements you will need to do as sid said and build a new array out of the non-blank elements. If all you were doing was printing them to the screen then this would have worked fine.

    PHP Code:
    <?
    if ($submit) {
        if(
    count($answer) > 0) {
            foreach(
    $answer as $key => $val) {
                if(
    $val != "") {
                    
    $answer[$key] = addslashes($val);
                    }
                else {
                    unset(
    $answer[$key]);
                    }
                }
            
    reset($answer);
            foreach(
    $answer as $key => $val) {
                print 
    "$key = $val<br>";
                }
            }
        else {
            print 
    "No answers submitted";
            }
        }
    ?>
    <form action="<?=$PHP_SELF?>" method="post">
    answer 1 <INPUT TYPE="text" NAME="answer[]" VALUE="" SIZE="30" MAXLENGTH="100"><br>
    answer 2 <INPUT TYPE="text" NAME="answer[]" VALUE="" SIZE="30" MAXLENGTH="100"><br>
    answer 3 <INPUT TYPE="text" NAME="answer[]" VALUE="" SIZE="30" MAXLENGTH="100"><br>
    answer 4 <INPUT TYPE="text" NAME="answer[]" VALUE="" SIZE="30" MAXLENGTH="100"><br>
    answer 5 <INPUT TYPE="text" NAME="answer[]" VALUE="" SIZE="30" MAXLENGTH="100"><br>
    <input type="submit" name="submit" value="Submit">
    </form>

    But you can make a simple adjustment easily like this:

    PHP Code:
    <?
    if ($submit) {
        if(
    count($answer) > 0) {
            foreach(
    $answer as $key => $val) {
                if(
    $val != "") {
                    
    $newAnswer[] = addslashes($val);
                    }
                else {
                    continue;
                    }
                }
            foreach(
    $newAnswer as $key => $val) {
                print 
    "$key = $val<br>";
                }
            }
        else {
            print 
    "No answers submitted";
            }
        }
    ?>
    <form action="<?=$PHP_SELF?>" method="post">
    answer 1 <INPUT TYPE="text" NAME="answer[]" VALUE="" SIZE="30" MAXLENGTH="100"><br>
    answer 2 <INPUT TYPE="text" NAME="answer[]" VALUE="" SIZE="30" MAXLENGTH="100"><br>
    answer 3 <INPUT TYPE="text" NAME="answer[]" VALUE="" SIZE="30" MAXLENGTH="100"><br>
    answer 4 <INPUT TYPE="text" NAME="answer[]" VALUE="" SIZE="30" MAXLENGTH="100"><br>
    answer 5 <INPUT TYPE="text" NAME="answer[]" VALUE="" SIZE="30" MAXLENGTH="100"><br>
    <input type="submit" name="submit" value="Submit">
    </form>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •