lets say i have a function that takes an array as one of its arguments:

function show_array ($array)
{
if ($array == $_POST) {echo 'the function argument is POST<br>'; print_r ($_POST);}
if ($array == $_SESSION) {echo 'the function argument is SESSION<br>'; print_r ($_SESSION);}
}

show_array ($_POST);

this does not work as expected. it seems that you cannot use the array name in conjuction with comparison operators. how can you access the name of the array in argument? i tried func_get_arg() but i have the same problem.

    try this:

    function show_array ($array)
    {
    if (in_array("$_POST", $array )) {echo 'the function argument is POST<br>'; print_r ($_POST);}
    if (in_array("$_SESSION",$array)) {echo 'the function argument is SESSION<br>'; print_r ($_SESSION);}
    }
    
    show_array ($_POST);
    

    you want to find in the array the values of $POST or $SESSION.

    http://www.php.net/manual/en/function.in-array.php

      I believe that you're trying to compare $array with the values of $_POST or any other global array to see if you are accessing that array or not. What you would have do is use something like array_diff() to compare the contents of the arrays. If they are the same, then you're working on the same array, ie: no differences.

      http://www.php.net/manual/en/function.array-diff.php

      -Steven

        i appreciate the responses but i don't think i properly explained what i am trying to do. i want to refer to the array by NAME, not by VALUE. that is to say, i want to check to see if the array arument to the function is called $POST, not is equal to $POST. i need to do this so i can run conditional code if the array argument is $POST or $SESSION.

        if (the argument array is called $_POST) {...do something..}

          No, it's not possible to know the name of the argument that was passed. One of the reasons is what happens if the array doesn't have a name? For example, this is syntactically correct:

          show_array(array(1,2,3));

          or

          show_array(explode("\n", $lines));

          or

          show_array(file("file.txt"));

          So if you were able to access the name of the argument, what name would the above have? They're anonymous...

          Anyway, I'm just saying that in PHP, or any other language that I know, you cannot get the name of the argument passed.

          The closes you can come to that is, like other said, comparing the values of the arrays.

          Diego

            thanks Diego

            i suppose a rather clumsy workaround is to pass the argument not as an array but as a string:

            function show_array ($string)
            {
            if ($string == '_POST') {echo 'the function argument is POST<br>'; print_r ($_POST);}
            if ($string == '_SESSION') {echo 'the function argument is SESSION<br>'; print_r ($_SESSION);}
            }
            
            show_array ('_POST');
            

            do you see any potential pitfalls to this?

              actually i think i found a pitfall already. this does not work:

              function list_array ($string)
              {
              foreach ($$string as $key => $value) {echo "$key => $value<br>";}
              }
              
              list_array ('_POST');
              list_array ('_GET');
              list_array ('_SESSION');
              

              why doesn't this work? isn't this just a simple use of variable variables?

                this is rather ugly but it does work:

                function list_array ($string)
                {
                $php = 'foreach ($' . $string . ' as $key => $value) {echo "$key => $value<br>";}';
                eval ($php);
                }
                
                list_array ('_POST');
                
                  Write a Reply...