How to pass array names to function parameters ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tony@tony.com

    How to pass array names to function parameters ?


    i'm trying to itterate through an array that contains the names of the
    global arrays

    eg:

    $myarray = array("\$_GET", "\$_SERVER" ); and so on

    The problem Im having is calling a function with

    myfunction($mya rray[0]);

    doesnt seem to pass the array name in a manner I can then use.

    I'm sure I'm missing something simple here can anyonehelp please ?

    tony
  • Scott

    #2
    Re: How to pass array names to function parameters ?

    On Sat, 2006-04-15 at 12:26 +0000, tony@tony.com wrote:[color=blue]
    > i'm trying to itterate through an array that contains the names of the
    > global arrays
    >
    > eg:
    >
    > $myarray = array("\$_GET", "\$_SERVER" ); and so on
    >
    > The problem Im having is calling a function with
    >
    > myfunction($mya rray[0]);
    >
    > doesnt seem to pass the array name in a manner I can then use.
    >
    > I'm sure I'm missing something simple here can anyonehelp please ?
    >
    > tony[/color]

    That should pass the name of the global array. You could also use single
    quotes and drop the escape character:

    $myarray = array('$_GET, '$_SERVER');

    Are you sure the problem isn't in your function? What are you doing
    with the name once your function receives it?

    Scott


    Comment

    • damirz@gmail.com

      #3
      Re: How to pass array names to function parameters ?

      Actually, you cannot use superglobal arrays as variable variables:
      [color=blue]
      >From PHP manual:[/color]


      "Variable variables: Superglobals cannot be used as variable variables
      inside functions or class methods."

      Comment

      • tony@tony.com

        #4
        Re: How to pass array names to function parameters ?

        In article <1145117876.799 548.249800@v46g 2000cwv.googleg roups.com>,
        damirz@gmail.co m says...[color=blue]
        > Actually, you cannot use superglobal arrays as variable variables:
        >[color=green]
        > >From PHP manual:[/color]
        > http://www.php.net/manual/en/languag...predefined.php
        >
        > "Variable variables: Superglobals cannot be used as variable variables
        > inside functions or class methods."
        >
        >[/color]

        Oh bugger.

        Thanks for that.

        I was just trying to get all the superglobals to echo out to the browser
        without having to code every one seperately...


        something like:

        function showthisarray($ nextGlobal) {
        echo($nextGloba l . "\n"); // debug line
        foreach($nextGl obal as $k => $v ) {
        echo($k . " = " . $v . "\n");
        }
        }

        The idea being to just pass the array names in sequence or as selected.

        The function does get the name of the relevant array as the first echo
        prints it ok eg . $_GET or whatever as is passed into $nextGlobal

        But the foreach() function just generates a bad parameter error.

        I guess it will have to be the long winded solution.

        tony

        Comment

        • Sidonath

          #5
          Re: How to pass array names to function parameters ?

          But you still can try with $HTTP_*_VARS :)

          // $nextGlobal = {"GET", "POST", ...}

          function showthisarray($ nextGlobal) {
          echo($nextGloba l . "\n"); // debug line
          foreach(${'HTTP _' . $nextGlobal . '_VARS'} as $k => $v ) {
          echo($k . " = " . $v . "\n");
          }
          }

          I didn't write much PHP lately, so sorry if there is some syntax (or
          any other) error here.

          Comment

          • tony@tony.com

            #6
            Re: How to pass array names to function parameters ?

            In article <1145134642.131 789.187380@j33g 2000cwa.googleg roups.com>,
            damirz@gmail.co m says...[color=blue]
            > But you still can try with $HTTP_*_VARS :)
            >
            > // $nextGlobal = {"GET", "POST", ...}
            >
            > function showthisarray($ nextGlobal) {
            > echo($nextGloba l . "\n"); // debug line
            > foreach(${'HTTP _' . $nextGlobal . '_VARS'} as $k => $v ) {
            > echo($k . " = " . $v . "\n");
            > }
            > }
            >
            > I didn't write much PHP lately, so sorry if there is some syntax (or
            > any other) error here.
            >
            >[/color]

            Worth a try but it produces the same result. It seems you can't pass the
            array name in a string at all from what I can see.

            tony


            Comment

            • Sidonath

              #7
              Re: How to pass array names to function parameters ?

              I knew that I forgot something :/

              $HTTP_*_VARS arrays are not superglobal. Hence you need to specify in
              function that you're using global arrays or access them with $GLOBALS
              array

              Here's a bit modified example that should work:

              ------------------------

              // custom global array
              $myArray = array("some", "elements") ;

              // array with names of global arrays you want to access
              $globalArrayNam es = array("HTTP_GET _VARS", "HTTP_POST_VARS ",
              "myArray");

              // parameter is global array name
              function showThisArray($ globalArrayName ) {
              echo($globalArr ayName . "\n"); // debug line
              foreach($GLOBAL S[$globalArrayNam e] as $k => $v ) {
              echo($k . " = " . $v . "\n");
              }
              }

              showThisArray($ globalArrayName s[0]);
              showThisArray($ globalArrayName s[1]);
              showThisArray($ globalArrayName s[2]);

              ------------------------

              Hope this helps

              Comment

              • tony@tony.com

                #8
                Re: How to pass array names to function parameters ?

                In article <1145189729.880 217.112930@i40g 2000cwc.googleg roups.com>,
                damirz@gmail.co m says...[color=blue]
                > I knew that I forgot something :/
                >[/color]

                Nice one. Modified slightly & tested - it works fine - as below.
                I added SERVER just to be sure of some output from a Sglobal
                So much nicer than having to do each one at a time ;-)

                <?php
                // custom global array
                $myArray = array("some", "elements") ;

                // array with names of global arrays you want to access
                $globalArrayNam es = array("HTTP_SER VER_VARS", "HTTP_POST_VARS ",
                "myArray");

                // parameter is global array name
                function showThisArray($ globalArrayName ) {
                echo("<br><br>" . "[ " . $globalArrayNam e . " ]<br>");
                foreach($GLOBAL S[$globalArrayNam e] as $k => $v ) {
                echo($k . " = " . $v . "<br>");
                }
                }

                showThisArray($ globalArrayName s[0]);
                showThisArray($ globalArrayName s[1]);
                showThisArray($ globalArrayName s[2]);
                ?>

                tony

                Comment

                • Ken Robinson

                  #9
                  Re: How to pass array names to function parameters ?


                  tony@tony.com wrote:[color=blue]
                  > In article <1145189729.880 217.112930@i40g 2000cwc.googleg roups.com>,
                  > damirz@gmail.co m says...[color=green]
                  > > I knew that I forgot something :/
                  > >[/color]
                  >
                  > Nice one. Modified slightly & tested - it works fine - as below.
                  > I added SERVER just to be sure of some output from a Sglobal
                  > So much nicer than having to do each one at a time ;-)
                  >[/color]

                  Here's another way of doing this.

                  <?php
                  // custom global array
                  $myArray = array("some", "elements") ;

                  // array with names of global arrays you want to access
                  $globalArrayNam es =
                  array(array('$_ SERVER',$_SERVE R),array('$_POS T',$_POST),
                  array("myArray" ,$myArray));

                  // parameter is global array name
                  function showThisArray($ globalArrayName ) {
                  echo("<br><br>" . "[ " . $globalArrayNam e[0] . " ]<br>");
                  echo '<pre>' . print_r($global ArrayName[1],true) . '</pre>';
                  }

                  showThisArray($ globalArrayName s[0]);
                  showThisArray($ globalArrayName s[1]);
                  showThisArray($ globalArrayName s[2]);
                  ?>


                  Ken

                  Comment

                  • tony@tony.com

                    #10
                    Re: How to pass array names to function parameters ?

                    In article <1145310837.226 085.226960@g10g 2000cwb.googleg roups.com>,
                    kenrbnsn@gmail. com says...
                    [color=blue]
                    > Here's another way of doing this.
                    >
                    > <?php
                    > // custom global array
                    > $myArray = array("some", "elements") ;
                    >
                    > // array with names of global arrays you want to access
                    > $globalArrayNam es =
                    > array(array('$_ SERVER',$_SERVE R),array('$_POS T',$_POST),
                    > array("myArray" ,$myArray));
                    >
                    > // parameter is global array name
                    > function showThisArray($ globalArrayName ) {
                    > echo("<br><br>" . "[ " . $globalArrayNam e[0] . " ]<br>");
                    > echo '<pre>' . print_r($global ArrayName[1],true) . '</pre>';
                    > }
                    >
                    > showThisArray($ globalArrayName s[0]);
                    > showThisArray($ globalArrayName s[1]);
                    > showThisArray($ globalArrayName s[2]);
                    > ?>
                    >
                    >
                    > Ken
                    >
                    >[/color]

                    And there was me thinking it couldnt be done ... ;-)
                    Now we have two ways.

                    Correct me if I'm wrong though Ken but doesnt this method pass the actual
                    array rather than just the name?

                    I'm not sure because I'm not able to work out what this is doing exactly
                    (I'm new to PHP as you may have gathered)

                    tony

                    Comment

                    • milahu

                      #11
                      Re: How to pass array names to function parameters ?

                      tony@tony.com schrieb:
                      [color=blue]
                      > i'm trying to itterate through an array that contains the names of the
                      > global arrays
                      >
                      > eg:
                      >
                      > $myarray = array("\$_GET", "\$_SERVER" ); and so on
                      >
                      > The problem Im having is calling a function with
                      >
                      > myfunction($mya rray[0]);
                      >
                      > doesnt seem to pass the array name in a manner I can then use.
                      >
                      > I'm sure I'm missing something simple here can anyonehelp please ?
                      >
                      > tony[/color]

                      Try this one:

                      $arrs = array('_GET', '_POST');
                      foreach($arrs as $arrnam)
                      print_r($$arrna m);

                      Comment

                      Working...