accessing arrays

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

    accessing arrays

    I'm sure there is an FAQ on this somewhere, but I couldn't find one.
    I'm sure a responder will clue me in.

    my URL is: mypage.php?var= 2&var=4&var=5

    print_r($_GET);

    gives me: 5

    instead of

    Array (
    [var] => 2
    [var] => 4
    [var] => 5
    )

    I tried:

    print_r($get[var]);

    but this gave me just 5 also. How do I access multiple values (from a
    multi-select list)??

    Mark
  • Justin Koivisto

    #2
    Re: accessing arrays

    Rainman wrote:[color=blue]
    > I'm sure there is an FAQ on this somewhere, but I couldn't find one. I'm
    > sure a responder will clue me in.
    >
    > my URL is: mypage.php?var= 2&var=4&var=5
    >
    > print_r($_GET);
    >
    > gives me: 5
    >
    > instead of
    >
    > Array (
    > [var] => 2
    > [var] => 4
    > [var] => 5
    > )
    >
    > I tried:
    >
    > print_r($get[var]);
    >
    > but this gave me just 5 also. How do I access multiple values (from a
    > multi-select list)??[/color]

    Name the list "var[]" instead of "var"...

    --
    Justin Koivisto, ZCE - justin@koivi.co m

    Comment

    • Rainman

      #3
      Re: accessing arrays

      > Name the list "var[]" instead of "var"...

      Thanks for contributing a reply, but I cannot decode your answer. And I
      still haven't found the answer to my situation.

      I'm not naming anything 'var' or 'var[]'/ Rather, I'm just using the
      $_GET or $_POST array to access what is supplied through GET or POST
      (thus far in my code). The 'var' part comes in the querystring (or from
      $_POST variables) (depending on the method I use in my form).

      Are you referring to the parent page that has the form?

      I tried what I think is your suggestion by doing this:

      $var[] = $_GET[var]; print_r($var[]);

      but this just gives me syntax errors.

      Other ideas?

      Mark

      Comment

      • ZeldorBlat

        #4
        Re: accessing arrays

        >>Name the list "var[]" instead of "var"...[color=blue]
        >Thanks for contributing a reply, but I cannot decode your answer. And I
        >still haven't found the answer to my situation.
        >
        >I'm not naming anything 'var' or 'var[]'/[/color]

        Sure you are. You provided the following URL:
        mypage.php?var= 2&var=4&var=5 In this case, you've named all three
        variables var. What would you expect the following code to output:

        $x = 2;
        $x = 4;
        $x = 5;
        echo $x;

        This will echo 5 because you've given a new value to $x on the second
        line, and overwrote it again on the third line. The same thing happens
        with your URL. The last value of var overwrites the first and second
        value. As such, you get var = 5 on mypage.php.

        You can do one of two things here: either give each variable a
        different name, like mypage.php?var1 =2&var2=4&var3= 5 in which case
        you'd refer to them as $_GET['var1'], $_GET['var2'], etc.

        Alternatively you can make var an array in the URL and access it like
        an array on mypage.php. For instance, your URL might look like this:
        mypage.php?var[0]=2&var[1]=4&var[2]=5. On mypage.php, you can get to
        those like this: $_GET['var'][0], $_GET['var'][1], etc.

        Comment

        • Etienne Marais

          #5
          Re: accessing arrays

          Rainman wrote:
          [color=blue][color=green]
          >> Name the list "var[]" instead of "var"...[/color]
          >
          > Thanks for contributing a reply, but I cannot decode your answer. And I
          > still haven't found the answer to my situation.[/color]

          I think he was trying to say

          <select name="var[]" ...

          instead of

          <select name="var" ....

          in the actual form.

          Comment

          • Rainman

            #6
            Re: accessing arrays

            Etienne Marais wrote:[color=blue]
            > Rainman wrote:
            >
            >[color=green][color=darkred]
            >>>Name the list "var[]" instead of "var"...[/color]
            >>
            >>Thanks for contributing a reply, but I cannot decode your answer. And I
            >>still haven't found the answer to my situation.[/color]
            >
            >
            > I think he was trying to say
            >
            > <select name="var[]" ...
            >
            > instead of
            >
            > <select name="var" ....
            >
            > in the actual form.[/color]

            Ah ha! Yes indeed! This works! Thanks for clarifying!
            Mark

            Comment

            • Justin Koivisto

              #7
              Re: accessing arrays

              Rainman wrote:[color=blue]
              > Etienne Marais wrote:
              >[color=green]
              >> Rainman wrote:
              >>[color=darkred]
              >>>> Name the list "var[]" instead of "var"...
              >>>
              >>> Thanks for contributing a reply, but I cannot decode your answer. And I
              >>> still haven't found the answer to my situation.[/color]
              >>
              >> I think he was trying to say
              >> <select name="var[]" ...
              >> instead of
              >> <select name="var" ....
              >> in the actual form.[/color]
              >
              > Ah ha! Yes indeed! This works! Thanks for clarifying!
              > Mark[/color]

              Sorry, I was on my way out the door after a long day... I should have
              elaborated a bit on that.

              --
              Justin Koivisto, ZCE - justin@koivi.co m

              Comment

              Working...