using [] inside eval function

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

    using [] inside eval function

    Hi All,

    I'm using eval and arrays in foreach and have trouble with adding
    elements
    to them - I'm talking about the '[]' operator.

    My code is:

    // creates arrays with the names of columns in keys array
    foreach ($keys as $k) {
    eval("\$$k = array();");
    }
    // fills dynamically columns' arrays with its values
    foreach ($array as $a) {
    foreach ($keys as $k) {
    // parse error
    //eval("\$$k[] = $a[$k];");
    // works fine
    eval("array_pus h(\$$k, $a[$k]);");
    }
    }

    I used array_push and it works fine but...

    'Note: If you use array_push() to add one element to the array it's
    better to use $array[] = because in that way there is no overhead of
    calling a function.'

    It's not a matter of life and death but is there some way to use the []
    inside the eval
    function?

    thanks in advance for Your help
    best regards R

  • Kimmo Laine

    #2
    Re: using [] inside eval function

    "R" <ruthless@poczt a.onet.plwrote in message
    news:1153995282 .205183.296370@ 75g2000cwc.goog legroups.com...
    Hi All,
    >
    I'm using eval and arrays in foreach and have trouble with adding
    elements
    to them - I'm talking about the '[]' operator.
    >
    My code is:
    >
    // creates arrays with the names of columns in keys array
    foreach ($keys as $k) {
    eval("\$$k = array();");
    }
    $$k = array();
    works fine, no reason to use eval.
    // fills dynamically columns' arrays with its values
    foreach ($array as $a) {
    foreach ($keys as $k) {
    // parse error
    //eval("\$$k[] = $a[$k];");
    Again, just use:
    $$k[] = $a[$k];
    no eval needed here either.
    // works fine
    eval("array_pus h(\$$k, $a[$k]);");
    And here too:
    array_push($$k, $a[$k]);
    }
    }
    >
    I used array_push and it works fine but...
    Your home assignment is to study a wonderful language feature called
    "variable variables":


    --
    "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
    http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
    spam@outolempi. net || Gedoon-S @ IRCnet || rot13(xvzzb@bhg byrzcv.arg)


    Comment

    • Erwin Moller

      #3
      Re: using [] inside eval function

      R wrote:
      Hi All,
      >
      I'm using eval and arrays in foreach and have trouble with adding
      elements
      to them - I'm talking about the '[]' operator.
      >
      My code is:
      >
      // creates arrays with the names of columns in keys array
      foreach ($keys as $k) {
      eval("\$$k = array();");
      Why are you using eval here?

      $$k = array();
      is doing what you want.

      }
      // fills dynamically columns' arrays with its values
      foreach ($array as $a) {
      foreach ($keys as $k) {
      // parse error
      //eval("\$$k[] = $a[$k];");
      // works fine
      eval("array_pus h(\$$k, $a[$k]);");
      }
      }

      Stop using eval and you are fine.
      >
      I used array_push and it works fine but...
      >
      'Note: If you use array_push() to add one element to the array it's
      better to use $array[] = because in that way there is no overhead of
      calling a function.'
      >
      It's not a matter of life and death but is there some way to use the []
      inside the eval
      function?
      >
      thanks in advance for Your help
      best regards R

      Regards,
      Erwin Moller

      Comment

      • R

        #4
        Re: using [] inside eval function

        Kimmo Laine wrote:
        Again, just use:
        $$k[] = $a[$k];
        PHP5: Fatal error: Cannot use [] for reading

        but $k is not empty and

        echo $a[$k] works fine...

        when I changed above line to:

        $$k[] = 12345;

        PHP5: Fatal error: Cannot use [] for reading

        any idea?

        best regards
        R

        Comment

        • Kimmo Laine

          #5
          Re: using [] inside eval function

          "R" <ruthless@poczt a.onet.plwrote in message
          news:1153998135 .175703.276200@ m73g2000cwd.goo glegroups.com.. .
          Kimmo Laine wrote:
          >Again, just use:
          >$$k[] = $a[$k];
          >
          PHP5: Fatal error: Cannot use [] for reading
          >
          but $k is not empty and
          >
          echo $a[$k] works fine...
          >
          when I changed above line to:
          >
          $$k[] = 12345;
          >
          PHP5: Fatal error: Cannot use [] for reading
          >
          any idea?

          Damn, I forgot about that. It should be:

          ${$k}[] = 12345;

          That should work.

          --
          "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
          http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
          spam@outolempi. net || Gedoon-S @ IRCnet || rot13(xvzzb@bhg byrzcv.arg)


          Comment

          • Mladen Gogala

            #6
            Re: using [] inside eval function

            Kimmo Laine wrote:
            Your home assignment is to study a wonderful language feature called
            "variable variables":
            http://php.net/manual/en/language.va...s.variable.php
            That is so much better and more secure then the \$k construct from
            another language, the one that would allow you something like:

            my $class=shift;
            bless \$k $class;

            I must say that I really love PHP. If only there was a Comprehensive PHP
            Archive Network...... Speaking of CPAN, did anybody use this:






            --
            Mladen Gogala

            Comment

            Working...