> For the complete documentation index, see [llms.txt](https://minwork.gitbook.io/array/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://minwork.gitbook.io/array/manipulating-array/mapping.md).

# Mapping

## map

#### Definition

```php
Arr::map(array $array, callable $callback, int $mode = Arr::MAP_ARRAY_KEY_VALUE): array
```

#### Description

Applies a callback to the elements of given array. Arguments supplied to callback differs depending on selected `$mode`.

{% hint style="warning" %}
For backward compatibility using `map(callable, array)` is still possible but is deprecated and will issue appropriate warning
{% endhint %}

#### Modes

| Constant name                  | Description                                                                                     |
| ------------------------------ | ----------------------------------------------------------------------------------------------- |
| MAP\_ARRAY\_KEY\_VALUE         | <p>Map array using callback in form of <br><code>function($key, $value)</code></p>              |
| MAP\_ARRAY\_VALUE\_KEYS\_LIST  | <p>Map array using callback in form of <br><code>function($value, $key1, $key2, ...)</code></p> |
| MAP\_ARRAY\_KEYS\_ARRAY\_VALUE | <p>Map array using callback in form of <br><code>function(array $keys, $value)</code></p>       |
| MAP\_ARRAY\_VALUE\_KEY         | <p>Map array using callback in form of <br><code>function($value, $key)</code></p>              |

#### Examples

```php
$array1 = ['a', 'b', 'c'];
$array2 = [
    1 => [
        2 => 'a',
        3 => 'b',
        4 => [
            5 => 'c',
        ],
    ],
    'test' => 'd',
];

$mapKeyValue = function ($key, $value) {
    return "{$key} -> {$value}";
};
// Mind that $value is a first argument here
$mapValueKey = function ($value, $key) {
    return "{$key} -> {$value}";
};
$mapKeysValue = function ($keys, $value) {
    return implode('.', $keys) . " -> {$value}";
};
$mapValueKeysList = function ($value, $key1, $key2) {
    return "$key1.$key2 -> {$value}";
};

// Equivalent to using MAP_ARRAY_KEY_VALUE as mode (3rd) argument
Arr::map($array1, $mapKeyValue) -> ['0 -> a', '1 -> b', '2 -> c']

// Resemble array_map function but with array supplied as first argument
Arr::map($array1, $mapValueKey) -> ['0 -> a', '1 -> b', '2 -> c']

// Map multidimensional array using keys array
Arr::map($array2, $mapKeysValue, Arr::MAP_ARRAY_KEYS_ARRAY_VALUE) ->
[
    1 => [
        2 => '1.2 -> a',
        3 => '1.3 -> b',
        4 => [
            5 => '1.4.5 -> c',
        ],
    ],
    'test' => 'test -> d',
]

// Map multidimensional array using keys list (mind that all keys above 2nd are ignored due to callback function syntax)
Arr::map($array2, $mapValueKeysList, Arr::MAP_ARRAY_VALUE_KEYS_LIST) ->
[
    1 => [
        2 => '1.2 -> a',
        3 => '1.3 -> b',
        4 => [
            5 => '1.4 -> c',
        ],
    ],
    'test' => 'test -> d',
]
```

## mapObjects

#### Definition

```php
Arr::mapObjects(array $objects, string $method, ...$args): array
```

#### Description

Map array of object to values returned from objects method

#### Examples

```php
$object = new class() { 
    function test($arg = 0) { 
        return 1 + $arg; 
    }
};
$array = [$object, $object, $object];

Arr::mapObjects($array, 'test') -> [1, 1, 1]
Arr::mapObjects($array, 'test', 2) -> [3, 3, 3]
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://minwork.gitbook.io/array/manipulating-array/mapping.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
