Variable in Array

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

    Variable in Array

    Is it possible to put Variable in Array? Here is what I need:

    $array = Array($a,$b);

    $a = 'something';
    $b = 'something else';

    foreach ($array as $val) {
    echo "$val";
    }

    Output should be:

    something
    something else

    Array is:

    Array
    (
    [0] => $a
    [1] => $b
    )

    If this is not possible is there any other way to do this? Thing is that
    I need to display some data based on array content.

    So far I have used:

    if (in_array('5', $array)) {

    echo 'data that should be display based on array value. I cant put this
    data in array because array is stored in cookie and then read';

    }
  • Berislav Lopac

    #2
    Re: Variable in Array

    dr. zoidberg wrote:[color=blue]
    > Is it possible to put Variable in Array? Here is what I need:
    >
    > $array = Array($a,$b);
    >
    > $a = 'something';
    > $b = 'something else';
    >
    > foreach ($array as $val) {
    > echo "$val";
    > }
    >
    > Output should be:
    >
    > something
    > something else
    >
    > Array is:
    >
    > Array
    > (
    > [0] => $a
    > [1] => $b
    > )
    >
    > If this is not possible is there any other way to do this? Thing is
    > that I need to display some data based on array content.
    >
    > So far I have used:
    >
    > if (in_array('5', $array)) {
    >
    > echo 'data that should be display based on array value. I cant put
    > this data in array because array is stored in cookie and then read';
    >
    > }[/color]

    Use associative arrays, i.e. use keys:

    $array = array('a' => 'something',
    'b' => 'something else');

    Now you can have:

    echo array['a'];

    Berislav

    --
    If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
    Groucho, Chico, and Harpo, then Usenet is Zeppo.


    Comment

    • dr. zoidberg

      #3
      Re: Variable in Array

      Berislav Lopac wrote:[color=blue]
      > dr. zoidberg wrote:
      >[color=green]
      >>Is it possible to put Variable in Array? Here is what I need:
      >>
      >>$array = Array($a,$b);
      >>
      >>$a = 'something';
      >>$b = 'something else';
      >>
      >>foreach ($array as $val) {
      >>echo "$val";
      >>}
      >>
      >>Output should be:
      >>
      >>something
      >>something else
      >>
      >>Array is:
      >>
      >>Array
      >>(
      >> [0] => $a
      >> [1] => $b
      >>)
      >>
      >>If this is not possible is there any other way to do this? Thing is
      >>that I need to display some data based on array content.
      >>
      >>So far I have used:
      >>
      >>if (in_array('5', $array)) {
      >>
      >>echo 'data that should be display based on array value. I cant put
      >>this data in array because array is stored in cookie and then read';
      >>
      >>}[/color]
      >
      >
      > Use associative arrays, i.e. use keys:
      >
      > $array = array('a' => 'something',
      > 'b' => 'something else');
      >
      > Now you can have:
      >
      > echo array['a'];[/color]

      Like I said, I will save that Array into cookie and I cant save Array
      values, they would be too big for cookie.

      Comment

      • Pedro Graca

        #4
        Re: Variable in Array

        dr. zoidberg wrote:[color=blue]
        > Is it possible to put Variable in Array? Here is what I need:
        >
        > $array = Array($a,$b);
        >
        > $a = 'something';
        > $b = 'something else';
        >
        > foreach ($array as $val) {
        > echo "$val";
        > }
        >
        > Output should be:
        >
        > something
        > something else[/color]

        Maybe variable variables are what you're after:

        <?php
        $array = array('a', 'b');
        $a = 'something';
        $b = 'something else';

        foreach ($array as $val) {
        echo ${$val}, "\n";
        }
        ?>


        Reference: http://www.php.net/manual/en/languag...s.variable.php

        --
        USENET would be a better place if everybody read: : mail address :
        http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
        http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
        http://www.expita.com/nomime.html : to 10K bytes :

        Comment

        • Berislav Lopac

          #5
          Re: Variable in Array

          Pedro Graca wrote:[color=blue]
          > dr. zoidberg wrote:[color=green]
          >> Is it possible to put Variable in Array? Here is what I need:
          >>
          >> $array = Array($a,$b);
          >>
          >> $a = 'something';
          >> $b = 'something else';
          >>
          >> foreach ($array as $val) {
          >> echo "$val";
          >> }
          >>
          >> Output should be:
          >>
          >> something
          >> something else[/color]
          >
          > Maybe variable variables are what you're after:
          >
          > <?php
          > $array = array('a', 'b');
          > $a = 'something';
          > $b = 'something else';
          >
          > foreach ($array as $val) {
          > echo ${$val}, "\n";
          > }[/color]

          Even better:

          $array = array('a', 'b');
          $values = array('a' => 'something',
          'b' => 'something else');

          foreach ($array as $val) {
          echo $values[$val]. "\n";
          }

          That way you don't infest your code with too many variables.

          Berislav


          --
          If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
          Groucho, Chico, and Harpo, then Usenet is Zeppo.


          Comment

          • Erwin Moller

            #6
            Re: Variable in Array

            dr. zoidberg wrote:

            <snip>
            [color=blue]
            >
            > Like I said, I will save that Array into cookie and I cant save Array
            > values, they would be too big for cookie.[/color]

            No, you didn't. You echoed that.
            And why can you not store an array in a cookie?

            If you have too much data for a cookie, the way you store it inside the
            coocie won'y help you a lot.

            If you have too much data for a cookie, use a session.

            Regards,
            Erwin Moller

            Comment

            • dr zoidberg

              #7
              [solved] Re: Variable in Array

              Pedro Graca wrote:[color=blue]
              > dr. zoidberg wrote:
              >[color=green]
              >>Is it possible to put Variable in Array? Here is what I need:
              >>
              >>$array = Array($a,$b);
              >>
              >>$a = 'something';
              >>$b = 'something else';
              >>
              >>foreach ($array as $val) {
              >>echo "$val";
              >>}
              >>
              >>Output should be:
              >>
              >>something
              >>something else[/color]
              >
              >
              > Maybe variable variables are what you're after:
              >
              > <?php
              > $array = array('a', 'b');
              > $a = 'something';
              > $b = 'something else';
              >
              > foreach ($array as $val) {
              > echo ${$val}, "\n";
              > }
              > ?>[/color]

              TNX!!

              Comment

              • Pedro Graca

                #8
                Re: Variable in Array

                Berislav Lopac wrote:[color=blue]
                > Pedro Graca wrote:[color=green]
                >> [... inferior "solution" snipped][/color][/color]
                [color=blue]
                > Even better:
                >
                > $array = array('a', 'b');
                > $values = array('a' => 'something',
                > 'b' => 'something else');
                >
                > foreach ($array as $val) {
                > echo $values[$val]. "\n";
                > }[/color]

                I wouldn't say this is even better;
                I'd say this is a million times much better!



                One more tip to add to bag-of-tricks:

                *----------------------------------------------------------*
                * Add another level of indirection and simplify your code. *
                *----------------------------------------------------------*



                Thank you, Berislav
                --
                USENET would be a better place if everybody read: : mail address :
                http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
                http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
                http://www.expita.com/nomime.html : to 10K bytes :

                Comment

                • dr. zoidberg

                  #9
                  Re: Variable in Array

                  Erwin Moller wrote:[color=blue]
                  > dr. zoidberg wrote:
                  >
                  > <snip>
                  >[color=green]
                  >>Like I said, I will save that Array into cookie and I cant save Array
                  >>values, they would be too big for cookie.[/color]
                  >
                  >
                  > No, you didn't. You echoed that.
                  > And why can you not store an array in a cookie?
                  >
                  > If you have too much data for a cookie, the way you store it inside the
                  > coocie won'y help you a lot.
                  >
                  > If you have too much data for a cookie, use a session.[/color]

                  Data needs to be retrieved againg, so session is not an option. Anyway
                  Pedro Grace gave me great tip, I didn't know about variable variables
                  before. Instead od puting while string info array (to much data), I only
                  put one letter per string.

                  Tnx all.

                  Comment

                  • Garp

                    #10
                    Re: Variable in Array

                    "dr. zoidberg" <linux@ns.cis.u 7.da.ru> wrote in message
                    news:c4g4uh$2i7 jab$1@ID-93631.news.uni-berlin.de...[color=blue]
                    > Is it possible to put Variable in Array? Here is what I need:
                    >[/color]
                    <snip>

                    On top of everything else, you can tinker with serialization - that's what
                    it's for.

                    This should work:

                    setcookie('myar ray',serialize( $myarray));
                    .....
                    $myarray=unseri alize($_COOKIE['myarray']);

                    HTH
                    Garp


                    Comment

                    Working...