1

I'm trying to apply a function to every item in an array in PHP. However, this function is contained in another file, containing a class.

I have included the file, and the function can be called easily enough. It just won't work with array_walk. The way I'm trying to do this is...

$new_variable_array = array_walk($variable_array, $class->function);
Allenph
  • 1,875
  • 27
  • 46
  • You're putting () at the end of the function, right? As long as you have instantiated the class - $class = new Class; - and then you could just - $new_variable_array = array_walk($variable_array, $class->function()); – Chris Evans May 06 '15 at 02:08
  • I hadn't been doing that, no. $new_variable_array = array_walk($variable_array, $class->function()); doesn't work either. – Allenph May 06 '15 at 02:23

1 Answers1

2

Ah sorry it's array_walk, ignore my comments, here is an answer. Since in array_walk you show the function name as a string containing the function name, when using a class method you need to do it inside an array, where the first element of the array is the class to be used, and the 2nd part is a string containing the method's name. I hope this works for you.

<?php

//This is my class
$myClass = new MyClass;

//This is my array
$variable_array = array();

//Array walk like so
array_walk($variable_array, array($myClass, 'functionName'));

Here is a working example, check that yours fits the same rules / parameters.

<?php

//My class
class myClass{
    public function addSurname(&$name, $key, $surname){
        $name = "$name $surname";
    }
}

//Instantiate the class
$myClass = new myClass;

//Array
$array = array('John', 'Mary', 'Jimbob', 'Juan');

//Add the surname to each array element
array_walk($array, array($myClass, 'addSurname'), 'Jones');

//Show the array
foreach($array as $x){
    echo $x . '<br />';
}
Chris Evans
  • 1,235
  • 10
  • 14
  • Hmm. This doesn't seem to be working for me either. I applied the same function [$class->function()] to a variable I assigned to a value contained in the $variable_array array. It worked flawlessly...there are just hundreds of them I'd like to use array_walk. – Allenph May 06 '15 at 02:42
  • I've added a working example of what I mean, check that yours fits. – Chris Evans May 06 '15 at 02:48
  • It should be exactly the same. $variable_array is an array which is created when a string is exploded by a deliminator, it is then fed through this EXACT code...array_walk($variable_array, array($class, 'function'));... From there I call each item in the array by...$variable = $variable_array["0"] Etc...If I use the exact same function on every instance of the variable declaration {I.E. $variable = $session->safe($variable_array["0"])} It works perfectly. – Allenph May 06 '15 at 03:03
  • If you have your function outside of a method, does it work with array_walk? Trying to figure out if it's your method that is the issue, as it doesn't work with array_walk, or if it's something else. – Chris Evans May 06 '15 at 03:10
  • I got it. My fault. I forgot to make the variable being fed to the method public. Adding & to my method fixed the problem. – Allenph May 06 '15 at 03:13