array_map and pass 2 arguments to the mapped function - array_map(): Argument #3 should be an array

Latheesan

I have an abstract class that looks like this:

abstract class Transformer {

    /**
     * Transform a collection of items
     *
     * @param array $items
     * @param bool $format
     * @return array
     */
    public function transformCollection(array $items, $format)
    {
        return array_map([$this, 'transform'], $items, $format);
    }

    /**
     * Transform a item
     *
     * @param array $item
     * @param bool $format
     * @return mixed
     */
    public abstract function transform(array $item, $format);

}

Then I have the following class that implements it:

class ServiceLogTransformer extends Transformer {

    public function transform(array $service_log, $format = false)
    {
        return [
            'id'    => $service_log['id'],
            'date'  => $service_log['log_date'],
            'time'  => $service_log['log_time'],
            'type'  => ($format ? status_label($service_log['log_type']) : $service_log['log_type']),
            'entry' => $service_log['log_entry']
        ];
    }

}

When this code runs, I get the error:

array_map(): Argument #3 should be an array

How do you pass 2 or more arguments when you call array_map function within a class? I checked the PHP Documentation and it looks like this is allowed, but it isn't working on my Larave 4.2 project.

Any ideas?

Peter

Please always read docs:

http://php.net/manual/en/function.array-map.php

array array_map ( callable $callback , array $array1 [, array $... ] )

and yet you pass bool $format as argument

"How do you pass 2 or more arguments when you call array_map function within a class?

I would create anonymous function with use() syntax

public function transformCollection(array $items, $format)
{
    return array_map(function($item) use ($format) {
        return $this->transform($item, $format);
    }, $items);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

array_map(): Argument #2 should be an array

From Javascript

array_map(): Argument #2 should be an array(one to one relation update)

From Dev

Passing two arguments to the function inside of array_map

From Dev

Using array_map with multi-argument function?

From Dev

Array_map() a simple cURL function

From Dev

How can I use parameters/arguments for my callback function in array_map()?

From Dev

How to apply array_map to a function which has more arguments in PHP?

From

Performance of foreach, array_map with lambda and array_map with static function

From Dev

PHP array_map callback function inside function

From Dev

TypeError: array_map(): Argument #1 ($callback) must be a valid callback or null, function "test" not found or invalid function name

From Dev

Setting callback function in array_map giving error

From Dev

Purpose of PHP use keyword in array_map() function?

From Dev

Skip iteration of array_map function if IF statement True

From Dev

array_map with access to keys

From Dev

PHP - array_map 2 columns and remove duplicate rows by key

From Dev

Ltrim on a array using array_map in Php

From Dev

Array_column an array_map return

From Dev

array_map - "Argument passed must be of the type array, string given" error

From Dev

How to pass standard php functions to array_map and array_filter?

From Dev

php array_map is not mapping arrays correctly

From

PHP's array_map including keys

From Dev

PHP - Is array_map faster than foreach?

From Dev

PHP array_map trim + parameters

From Dev

php array_map with static method of object

From Dev

Multiple functions using array_map

From Dev

array_map with two different classes?

From Dev

array_map unlink file named ..txt

From Dev

Simplify calling method on item in array_map

From Dev

array_map produces weird unicode character

Related Related

  1. 1

    array_map(): Argument #2 should be an array

  2. 2

    array_map(): Argument #2 should be an array(one to one relation update)

  3. 3

    Passing two arguments to the function inside of array_map

  4. 4

    Using array_map with multi-argument function?

  5. 5

    Array_map() a simple cURL function

  6. 6

    How can I use parameters/arguments for my callback function in array_map()?

  7. 7

    How to apply array_map to a function which has more arguments in PHP?

  8. 8

    Performance of foreach, array_map with lambda and array_map with static function

  9. 9

    PHP array_map callback function inside function

  10. 10

    TypeError: array_map(): Argument #1 ($callback) must be a valid callback or null, function "test" not found or invalid function name

  11. 11

    Setting callback function in array_map giving error

  12. 12

    Purpose of PHP use keyword in array_map() function?

  13. 13

    Skip iteration of array_map function if IF statement True

  14. 14

    array_map with access to keys

  15. 15

    PHP - array_map 2 columns and remove duplicate rows by key

  16. 16

    Ltrim on a array using array_map in Php

  17. 17

    Array_column an array_map return

  18. 18

    array_map - "Argument passed must be of the type array, string given" error

  19. 19

    How to pass standard php functions to array_map and array_filter?

  20. 20

    php array_map is not mapping arrays correctly

  21. 21

    PHP's array_map including keys

  22. 22

    PHP - Is array_map faster than foreach?

  23. 23

    PHP array_map trim + parameters

  24. 24

    php array_map with static method of object

  25. 25

    Multiple functions using array_map

  26. 26

    array_map with two different classes?

  27. 27

    array_map unlink file named ..txt

  28. 28

    Simplify calling method on item in array_map

  29. 29

    array_map produces weird unicode character

HotTag

Archive