Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2010
    Posts
    58
    Thanks
    10
    Thanked 3 Times in 3 Posts

    How to pass an array to a function and use it as an array

    I've been googling this for hours now and I can't find anything on it. What I want to do is create an array as a variable and use that as an argument for a function, so when I call the variable it outputs the array.

    This is for a WordPress snippet I'm trying to modify.

    Here's the Snippet
    PHP Code:
    function function my_css_attributes_filter($var) {
      return 
    is_array($var) ? array_intersect($var, array('current-menu-item','current-menu-ancestor')) : '';

    What I want to do is something like this.
    PHP Code:
    $the_list = array('current-menu-item','current-menu-ancestor');
    function function 
    my_css_attributes_filter($var$the_list) {
      return 
    is_array($var) ? array_intersect($var$the_list) : '';

    I now why this doesn't work, but I'm at a loss to what to do.

    I tried to create an array using a foreach loop like this
    PHP Code:
    function function my_css_attributes_filter($var$the_list) {
      
    $array = array();
      
    foraech($the_list as $value){
        
    $array[] = $value;
      }
      return 
    is_array($var) ? array_intersect($var$array) : '';

    but nothing was populated in the array.

    Any help would be appreciated.
    Last edited by gilgimech; 01-16-2013 at 05:23 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Uhh, if you pass an array to a function, it is an array.
    How are you calling this and what are the values of $var and $the_list when called? You can use print_r or var_dump to evaluate them.

  • #3
    New Coder
    Join Date
    Mar 2010
    Posts
    58
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Fou-Lu, thanks for the replay.

    I dug into the core code of the function I'm adding the filter to and I found that it won't accept another argument. That's why it's not working.

    So, I had to make the variable global to use it.
    PHP Code:
    $the_list = array('current-menu-item','current-menu-ancestor'); 
    function function 
    my_css_attributes_filter($var$the_list) { 
      global 
    $the_list;
      return 
    is_array($var) ? array_intersect($var$the_list) : ''


  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Sorry, you mean that whatever is used to call this function isn't actually designed to provide the second argument?
    In this case, it would be fine to use a global. You'll need to remove the second argument in the signature though, since it will trigger an error if it doesn't get it as its specified as being required. Otherwise, global is a part of the blight that makes code difficult to debug and is best avoided. Since PHP cannot overload function signatures, if you are stuck in a particular signature to work with sometimes globalization is the only way to get there (particularly with dynamic data).


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •