in_array() wierdness with arrays inside of $_POST

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • AJ Z

    #1

    in_array() wierdness with arrays inside of $_POST

    I am using in_array() to search for a value ("other"), in order to validate
    a form.
    If I pass $_POST["interests"] as the array to search PHP says that it is an
    invalid datatype.
    It is an array and if I copy it to another variable (i.e. $list =
    $_POST["interests"];) it works fine.
    The same is also true of implode().

    Do you believe this is intended or is it a bug?
    --
    AJ Zmudosky
    There are no stupid questions, but there are a LOT of inquisitive idiots.
  • Cameron

    #2
    Re: in_array() wierdness with arrays inside of $_POST

    AJ Z wrote:[color=blue]
    > I am using in_array() to search for a value ("other"), in order to
    > validate a form.
    > If I pass $_POST["interests"] as the array to search PHP says that it is
    > an invalid datatype.
    > It is an array and if I copy it to another variable (i.e. $list =
    > $_POST["interests"];) it works fine.
    > The same is also true of implode().
    >
    > Do you believe this is intended or is it a bug?
    > --
    > AJ Zmudosky
    > There are no stupid questions, but there are a LOT of inquisitive idiots.[/color]

    $_POST['whatever'] isn't an array $_POST is an array, but yes you will
    have to make it into an array first using something like

    $data = explode(",", $_POST['interests']);

    then it's an array.

    ~Cameron

    Comment

    • Andy Hassall

      #3
      Re: in_array() wierdness with arrays inside of $_POST

      On Sun, 01 Feb 2004 17:50:37 +0000, Cameron <foo@bar.invali d> wrote:
      [color=blue]
      >AJ Z wrote:[color=green]
      >> I am using in_array() to search for a value ("other"), in order to
      >> validate a form.
      >> If I pass $_POST["interests"] as the array to search PHP says that it is
      >> an invalid datatype.
      >> It is an array and if I copy it to another variable (i.e. $list =
      >> $_POST["interests"];) it works fine.
      >> The same is also true of implode().
      >>
      >> Do you believe this is intended or is it a bug?[/color]
      >
      >$_POST['whatever'] isn't an array $_POST is an array, but yes you will
      >have to make it into an array first using something like
      >
      >$data = explode(",", $_POST['interests']);
      >
      >then it's an array.[/color]

      $_POST['whatever'] _can_ be an array. The OP said it was.

      Consider:

      <input type="checkbox" name="whatever[]" value="one">
      <input type="checkbox" name="whatever[]" value="two">
      <input type="checkbox" name="whatever[]" value="three">

      --
      Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
      <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

      Comment

      • Andy Hassall

        #4
        Re: in_array() wierdness with arrays inside of $_POST

        On Sun, 1 Feb 2004 09:19:32 -0800, AJ Z <usenet@gwws.ne t> wrote:
        [color=blue]
        >I am using in_array() to search for a value ("other"), in order to validate
        >a form.
        >If I pass $_POST["interests"] as the array to search PHP says that it is an
        >invalid datatype.
        >It is an array and if I copy it to another variable (i.e. $list =
        >$_POST["interests"];) it works fine.
        >The same is also true of implode().
        >
        >Do you believe this is intended or is it a bug?[/color]

        Are you sure it's an array? Use var_dump, or is_array to check.

        Is it produced by a series of input elements named "whatever[]" (note the use
        of [] - required for PHP to produce an array)?

        --
        Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
        <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

        Comment

        • AJ Z

          #5
          Re: in_array() wierdness with arrays inside of $_POST

          On 2004-02-01 10:18:17 -0800, Andy Hassall <andy@andyh.co. uk> said:
          [color=blue]
          > On Sun, 1 Feb 2004 09:19:32 -0800, AJ Z <usenet@gwws.ne t> wrote:
          >[color=green]
          > >I am using in_array() to search for a value ("other"), in order to[/color][/color]
          validate >a form.[color=blue][color=green]
          > >If I pass $_POST["interests"] as the array to search PHP says that it is[/color][/color]
          an >invalid datatype.[color=blue][color=green]
          > >It is an array and if I copy it to another variable (i.e. $list =[/color]
          >$_POST["interests"];) it works fine.[color=green]
          > >The same is also true of implode().
          > >
          > >Do you believe this is intended or is it a bug?[/color]
          >
          > Are you sure it's an array? Use var_dump, or is_array to check.
          >
          > Is it produced by a series of input elements named "whatever[]" (note[/color]
          the use[color=blue]
          > of [] - required for PHP to produce an array)?
          >
          > -- Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
          > <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>
          >[/color]

          Yes, it is an array, I used is_array and var_dump at the suggestion of
          others, but I am wondering why it doesn't work as $_POST["interests"], but
          it does as $list (if $list = $_POST["interests"] -- it should be exactly
          the same). It is an array due to what you suggested to Cameron (checkboxes
          with a name of "something[]")
          --
          AJ Zmudosky
          There are no stupid questions, but there are a LOT of inquisitive idiots.

          Comment

          • Andy Hassall

            #6
            Re: in_array() wierdness with arrays inside of $_POST

            On Sun, 1 Feb 2004 13:39:48 -0800, AJ Z <usenet@gwws.ne t> wrote:
            [color=blue]
            >On 2004-02-01 10:18:17 -0800, Andy Hassall <andy@andyh.co. uk> said:
            >[color=green]
            >> On Sun, 1 Feb 2004 09:19:32 -0800, AJ Z <usenet@gwws.ne t> wrote:
            >>[color=darkred]
            >>> I am using in_array() to search for a value ("other"), in order to
            >>> validate a form.
            >>> If I pass $_POST["interests"] as the array to search PHP says that it is
            >>> an invalid datatype.
            >>> It is an array and if I copy it to another variable (i.e. $list =
            >>> $_POST["interests"];) it works fine.
            >>> The same is also true of implode().
            >>>
            >>> Do you believe this is intended or is it a bug?[/color]
            >>
            >> Are you sure it's an array? Use var_dump, or is_array to check.
            >>
            >> Is it produced by a series of input elements named "whatever[]" (note
            >>the use of [] - required for PHP to produce an array)?[/color]
            >
            >Yes, it is an array, I used is_array and var_dump at the suggestion of
            >others, but I am wondering why it doesn't work as $_POST["interests"], but
            >it does as $list (if $list = $_POST["interests"] -- it should be exactly
            >the same). It is an array due to what you suggested to Cameron (checkboxes
            >with a name of "something[]")[/color]

            Please post a small runnable example that demonstrates your problem.

            --
            Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
            <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>

            Comment

            • Rahul Anand

              #7
              Re: in_array() wierdness with arrays inside of $_POST

              Andy Hassall <andy@andyh.co. uk> wrote in message news:<67tq101kr 693h3b2hsmfknou ih5ob6q0km@4ax. com>...[color=blue]
              > On Sun, 1 Feb 2004 13:39:48 -0800, AJ Z <usenet@gwws.ne t> wrote:
              >[color=green]
              > >On 2004-02-01 10:18:17 -0800, Andy Hassall <andy@andyh.co. uk> said:
              > >[color=darkred]
              > >> On Sun, 1 Feb 2004 09:19:32 -0800, AJ Z <usenet@gwws.ne t> wrote:
              > >>
              > >>> I am using in_array() to search for a value ("other"), in order to
              > >>> validate a form.
              > >>> If I pass $_POST["interests"] as the array to search PHP says that it is
              > >>> an invalid datatype.
              > >>> It is an array and if I copy it to another variable (i.e. $list =
              > >>> $_POST["interests"];) it works fine.
              > >>> The same is also true of implode().
              > >>>
              > >>> Do you believe this is intended or is it a bug?
              > >>
              > >> Are you sure it's an array? Use var_dump, or is_array to check.
              > >>
              > >> Is it produced by a series of input elements named "whatever[]" (note
              > >>the use of [] - required for PHP to produce an array)?[/color]
              > >
              > >Yes, it is an array, I used is_array and var_dump at the suggestion of
              > >others, but I am wondering why it doesn't work as $_POST["interests"], but
              > >it does as $list (if $list = $_POST["interests"] -- it should be exactly
              > >the same). It is an array due to what you suggested to Cameron (checkboxes
              > >with a name of "something[]")[/color]
              >
              > Please post a small runnable example that demonstrates your problem.[/color]

              If it is verified by *is_array* that $_POST["interests"] is an array,
              then i guess you must be sending arguments in wrong order.
              Please verify the syntax of *in_array*:

              bool in_array ( mixed needle, array haystack [, bool strict] )

              post a sample code if it still not solved.

              --
              Cheers,
              Rahul Anand

              Comment

              • AJ Z

                #8
                Re: in_array() wierdness with arrays inside of $_POST

                On 2004-02-01 13:52:47 -0800, Andy Hassall <andy@andyh.co. uk> said:
                ...[color=blue]
                > Please post a small runnable example that demonstrates your problem.
                > --
                > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                > <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>[/color]
                <?php
                if ($_POST["action"] == "doprocform ") {
                // Remove excess spaces
                foreach ($_POST as $key => $val) { $_POST[$key] = trim($val); }
                // Validate form
                // Do other checks here
                if (in_array("othe r", $_POST["interests"]) && $_POST["other_explanat ion"]
                == "") { $err = true; }
                // Compress listing arrays
                $interests_list = implode(", ", $_POST["interests"]);
                if ($err) {
                // Display error message(s)
                } else {
                // Transmit form (via e-mail or to DB, etc.)
                }
                } else {
                // Display form (STND. HTML)
                }
                ?>
                The above use of in_array() and implode() generates warnings, under both
                PHP 4.3.3 and PHP 5.0.0b3.
                --
                AJ Zmudosky
                There are no stupid questions, but there are a LOT of inquisitive idiots.

                Comment

                • Rahul Anand

                  #9
                  Re: in_array() wierdness with arrays inside of $_POST

                  AJ Z <usenet@gwws.ne t> wrote in message news:<200402012 2060316807%usen et@gwwsnet>...[color=blue]
                  > On 2004-02-01 13:52:47 -0800, Andy Hassall <andy@andyh.co. uk> said:
                  > ..[color=green]
                  > > Please post a small runnable example that demonstrates your problem.
                  > > --
                  > > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                  > > <http://www.andyh.co.uk > / <http://www.andyhsoftwa re.co.uk/space>[/color]
                  > <?php
                  > if ($_POST["action"] == "doprocform ") {
                  > // Remove excess spaces
                  > foreach ($_POST as $key => $val) { $_POST[$key] = trim($val); }[/color]

                  The problem is here
                  *************** **************
                  $_POST[$key] = trim($val);
                  *************** **************

                  You can not trim an array. If you enable the notice errors, (a good
                  practice to catch errors during development) you will get an error
                  from PHP Parser.

                  In your case the $val will be converted to string and trim function
                  will return a string value "Array".

                  Modify the lines with:

                  foreach ($_POST as $key => $val)
                  {
                  if(!is_array($v al))
                  $_POST[$key] = trim($val);
                  }

                  [color=blue]
                  > // Validate form
                  > // Do other checks here
                  > if (in_array("othe r", $_POST["interests"]) && $_POST["other_explanat ion"]
                  > == "") { $err = true; }
                  > // Compress listing arrays
                  > $interests_list = implode(", ", $_POST["interests"]);
                  > if ($err) {
                  > // Display error message(s)
                  > } else {
                  > // Transmit form (via e-mail or to DB, etc.)
                  > }
                  > } else {
                  > // Display form (STND. HTML)
                  > }
                  > ?>
                  > The above use of in_array() and implode() generates warnings, under both
                  > PHP 4.3.3 and PHP 5.0.0b3.[/color]

                  Comment

                  • AJ Z

                    #10
                    Re: in_array() wierdness with arrays inside of $_POST

                    On 2004-02-01 22:25:57 -0800, rahulanand_bis@ rediffmail.com (Rahul Anand)
                    said:
                    ...[color=blue]
                    > post a sample code if it still not solved.
                    >
                    > --
                    > Cheers,
                    > Rahul Anand
                    >[/color]

                    Even with your suggestion on trim() I get the warning. Confirmed in_array()
                    args are in the correct order.
                    --
                    AJ Zmudosky
                    There are no stupid questions, but there are a LOT of inquisitive idiots.

                    Comment

                    • Rahul Anand

                      #11
                      Re: in_array() wierdness with arrays inside of $_POST

                      AJ Z <usenet@gwws.ne t> wrote in message news:<200402021 9572116807%usen et@gwwsnet>...[color=blue]
                      > On 2004-02-01 22:25:57 -0800, rahulanand_bis@ rediffmail.com (Rahul Anand)
                      > said:
                      > ..[color=green]
                      > > post a sample code if it still not solved.
                      > >
                      > > --
                      > > Cheers,
                      > > Rahul Anand
                      > >[/color]
                      >
                      > Even with your suggestion on trim() I get the warning. Confirmed in_array()
                      > args are in the correct order.[/color]

                      It works at my system [PHP 5]

                      [SNIP]

                      $_POST['key'] = array('aKey'=>' aValue','bKey'= >'bValue');

                      // Remove excess spaces
                      foreach ($_POST as $key => $val)
                      if(!is_array($v al))
                      $_POST[$key] = trim($val);

                      print_r($_POST) ;
                      exit;

                      [/SNIP]

                      Run this code to check it working. If it works, then receive the array
                      from a form post. I assure you that, it will execute smoothly.

                      You might have coded wrong, so please post the code you are trying to
                      execute.

                      --
                      Cheers,
                      Rahul Anand

                      Comment

                      • AJ Z

                        #12
                        Re: in_array() wierdness with arrays inside of $_POST

                        On 2004-02-03 00:59:23 -0800, rahulanand_bis@ rediffmail.com (Rahul Anand)
                        said:
                        ...

                        Your code does execute without errors, and shows up.
                        I also ran it through in_array (searching for 'aValue') and it generated no
                        warnings.
                        However, my array also returns true for is_array(), but gives the warning.
                        The entire source code for the form can be seen at:

                        If you would like it posted here I can trim it down and post it.
                        --
                        AJ Zmudosky
                        There are no stupid questions, but there are a LOT of inquisitive idiots.

                        Comment

                        • Rahul Anand

                          #13
                          Re: in_array() wierdness with arrays inside of $_POST

                          AJ Z <usenet@gwws.ne t> wrote in message news:<200402030 7093975249%usen et@gwwsnet>...[color=blue]
                          > On 2004-02-03 00:59:23 -0800, rahulanand_bis@ rediffmail.com (Rahul Anand)
                          > said:
                          > ..
                          >
                          > Your code does execute without errors, and shows up.
                          > I also ran it through in_array (searching for 'aValue') and it generated no
                          > warnings.
                          > However, my array also returns true for is_array(), but gives the warning.
                          > The entire source code for the form can be seen at:
                          > http://www.gwws.net/form.phps
                          > If you would like it posted here I can trim it down and post it.[/color]

                          I checked the code and found the reason for the error you are getting.
                          Here goes the explanation:

                          Line 10:
                          [SNIP]

                          if (is_array($_POS T["interests"])) { echo "'interests ' IS AN ARRAY<br
                          />\n"; } else { echo "'interests ' IS AN ARRAY<br />\n"; }

                          [/SNIP]

                          In both case you are printing "'interests ' IS AN ARRAY".
                          Change the else part, and you will notice *interests* is not array in
                          all cases.

                          Reason:
                          PHP forms an array only when there is atleast one checkbox checked.
                          Because a form only sends the checked checkbox values.

                          Solution:
                          Before trying to operate on intersts array you need to test the array
                          with *isempty* or *isset* or *isarray*

                          Hope it will help.

                          --
                          Rahul Anand

                          Comment

                          Working...