60

I am trying to create a class to handle arrays but I can't seem to get array_map() to work in it.

<?php
//Create the test array
$array = array(1,2,3,4,5,6,7,8,9,10);
//create the test class
class test {
//variable to save array inside class
public $classarray;

//function to call array_map function with the given array
public function adding($data) {
    $this->classarray = array_map($this->dash(), $data);
}

// dash function to add a - to both sides of the number of the input array
public function dash($item) {
    $item2 = '-' . $item . '-';
    return $item2;
}

}
// dumps start array
var_dump($array);
//adds line
echo '<br />';
//creates class object
$test = new test();
//classes function adding
$test->adding($array);
// should output the array with values -1-,-2-,-3-,-4-... 
var_dump($test->classarray);

This outputs

array(10) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(8) [8]=> int(9) [9]=> int(10) }

Warning: Missing argument 1 for test::dash(), called in D:\xampp\htdocs\trainingdvd\arraytesting.php on line 11 and defined in D:\xampp\htdocs\trainingdvd\arraytesting.php on line 15

Warning: array_map() expects parameter 1 to be a valid callback, function '--' not found or invalid function name in D:\xampp\htdocs\trainingdvd\arraytesting.php on line 11 NULL

What am I doing wrong or does this function just not work inside classes?

RavatSinh Sisodiya
  • 1,586
  • 1
  • 20
  • 41
Justin
  • 1,239
  • 1
  • 15
  • 36
  • possible duplicate of [Passing object method to array_map()](http://stackoverflow.com/questions/4546614/passing-object-method-to-array-map) – Gordon Mar 24 '11 at 16:22

7 Answers7

159

You are specifying dash as the callback in the wrong way.

This does not work:

$this->classarray = array_map($this->dash(), $data);

This does:

$this->classarray = array_map(array($this, 'dash'), $data);

Read about the different forms a callback may take here.

Jon
  • 413,451
  • 75
  • 717
  • 787
  • Thank you for the quick replay. I got it working and thanks for you help. I was just wondering do you happen to have more article about callback and how to specify if correctly? – Justin Mar 24 '11 at 16:49
  • @Justin: Take a look here: http://stackoverflow.com/questions/48947/how-do-i-implement-a-callback-in-php – Jon Mar 24 '11 at 19:01
  • 2
    even statically works like array_map( @[ self, 'dash' ] ) – bortunac Jun 25 '16 at 22:53
  • How can you pass a parameter with the dash function? – Nielsapp May 10 '18 at 11:33
  • 1
    @Nielsapp you cannot directly, you must pass in a callable that will effectively bind the arguments that you want, e.g. `array_map(function($item) { return $this->dash($item, 'additional argument'); }, $data)` – Jon May 14 '18 at 09:49
  • great. you're ans work . Thanks – Sumit patel May 04 '22 at 12:47
45

Hello You can use Like this one

    // Static outside of class context
array_map( array( 'ClassName', 'methodName' ), $array );

// Static inside class context
array_map( array( __CLASS__, 'methodName' ), $array );

// Non-static outside of object context
array_map( array( $object, 'methodName' ), $array );

// Non-static inside of object context
array_map( array( $this, 'methodName' ), $array );
Vijaysinh Parmar
  • 1,341
  • 1
  • 17
  • 20
2

It must read

$this->classarray = array_map(array($this, 'dash'), $data);

The array-thing is the PHP callback for a object instance method. Callbacks to regular functions are defined as simple strings containing the function name ('functionName'), while static method calls are defined as array('ClassName, 'methodName') or as a string like that: 'ClassName::methodName' (this works as of PHP 5.2.3).

Stefan Gehrig
  • 80,936
  • 24
  • 154
  • 184
  • Thank you for you answer. I was very helpful do you have happen to know of any more articles about this subject? Thanks again, – Justin Mar 24 '11 at 17:21
2

array_map($this->dash(), $data) calls $this->dash() with 0 arguments and uses the return value as the callback function to apply to each member of the array. You want array_map(array($this,'dash'), $data) instead.

Anomie
  • 89,707
  • 13
  • 124
  • 144
1

In case the class belongs to a different namespace, you need to use the complete namespaced class name. Below is an example using a CakePHP Utility class:

This will not work:

array_map(array('Inflector', 'humanize'), $some_array));

This will work:

array_map(array('Cake\Utility\Inflector', 'humanize'), $some_array));
Aris
  • 4,219
  • 1
  • 35
  • 36
0

For multidimensional arrays (any arrays):

    $data = array_map('decode'), $data);

    function decode($data)
    {
        if (is_array($data)) {
            foreach ($data as &$value) {
                if (is_array($value)) {
                    $value = decode($value);
                } else {
                    $value = html_entity_decode($value);
                }
            }
        } else {
            $data = html_entity_decode($data);
        }
        return $data;
    }

For multidimensional arrays (any arrays) in Class:

$data = array_map(array($this,'decode'), $data);

private function decode($data)
{
    if (is_array($data)) {
        foreach ($data as &$value) {
            if (is_array($value)) {
                $value = $this->decode($value);
            } else {
                $value = html_entity_decode($value);
            }
        }
    } else {
        $data = html_entity_decode($data);
    }
    return $data;
}
Format
  • 1
0

For those who wonder why array_map doesn't have access to outside variables like me, so variables must be passed using use. For example:

$param = 'some_value';

$ids = array_map(
    function($item) use ($param) { return $item[$param]; },
    $data
);