SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with array_walk and create_function

    Hi.

    I'm trying to make this function work:

    PHP Code:
    function array_preg_match($pattern$subject
    {
      
    $matches NULL;
      
    array_walk($subject
       
    create_function('$val, $key, &$array'
                       
    'if (preg_match("' $pattern '", "$val")) { $array[] = $key;}'),
       &
    $matches);
      return 
    $matches;

    At present, the function returns the correct array, but I get the error:
    Code:
    Warning: Call-time pass-by-reference has been deprecated - argument passed by value; If you would like to pass it by reference, modify the declaration of array_walk().
    I understand the error and what it means. However, I'd like to know if there is a way I can get round this.

    TIA
    ____________________


    George

  2. #2
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use array_filter instead.

    Use 'global'.

    Use another level of indirection:

    PHP Code:
    array_walk($subject$func, array(&$matches)); 
    =Austin
    Austin Hastings - Principal Consultant - Longacre, Inc.

    Anything you can do, you can do better.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm trying to make this function work:
    What about simply this:
    PHP Code:
    function array_preg_match($pattern$subject)
    {
        foreach(
    $subject as $key=>$value)
        {
            if(
    preg_match($pattern$value))
            {
                
    $matches[] = $key;
            }
        }
        return 
    $matches;


  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Austin_Hastings
    Use 'global'.
    =Austin
    How would that work? When I create a function with create_function(), I can't get 'global $matches' to refer to $matches in the surrounding function. For instance, this works:
    PHP Code:
    function test()
    {
        
    $func create_function('  $val, $key  '
                                
    '  echo "hello<br/>";  '
                               
    );
        
    $func(12);
    }
     
    test(); 
    but this doesn't:
    PHP Code:
    function test()
    {
        
    $matches 10;
        
    $func create_function('  $val, $key  '
                                
    '  echo "hello<br/>";  
                                   echo global $matches;  '
                               
    );
        
    $func(12);

    }
    test(); 
    Parse error: parse error, unexpected T_GLOBAL, expecting ',' or ';' in C:\Program Files\Apache Group\Apache2\htdocs\programs\sp array_walk create_function.php(21) : runtime-created function on line 2

    Fatal error: Call to undefined function: () in C:\Program Files\Apache Group\Apache2\htdocs\programs\sp array_walk create_function.php on line 22

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Austin_Hastings
    Use array_filter instead.

    Use 'global'.

    Use another level of indirection:

    PHP Code:
    array_walk($subject$func, array(&$matches)); 
    =Austin
    Thanks Austin - the last idea was the one!
    ____________________


    George

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do you need to use such a tortured construct? Were you able to discern that the function I posted does the exact same thing as your function?


Bookmarks

Posting Permissions

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