Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass more arguments to php array_walk?

Tags:

php

I want to know how to pass more arguments to my array_walk..

$addresses = array('www.google.com', 'www.yahoo.com', 'www.microsoft.com');
$a = 'hey';
$b = 'hey';
array_walk($addresses, array($this, '_handle'), $a, $b); // $a and $b parameters doesn't get passed

private function _handle($address,$a, $b) {
       echo $address; // www.google.com
       echo $a // 012
       echo $b // 012
}

How do I pass parameters anyway? I have to pass more than 5 parameters.. please teach me.. thanks!

like image 912
Kevin Lee Avatar asked Mar 23 '11 20:03

Kevin Lee


2 Answers

You can use the use keyword with an anonymous function like this:

Note: $custom_var is a mixed datatype so can be an array if you want to pass several values

$custom_var = 'something';

array_walk( 
      $array, 
      function( &$value, $key) use ( $custom_var ){ 
    
      // Your code here, you can access the value of $custom_var
    
      });
like image 196
pirooz jenabi Avatar answered Oct 16 '22 20:10

pirooz jenabi


It will only allow one argument for user data. I suggest passing your values as an array.

array_walk($addresses, array($this, '_handle'), array($a, $b));
like image 43
John Giotta Avatar answered Oct 16 '22 20:10

John Giotta