# mapAssoc()

{% code title="Example.php" %}

```php
<?php use kim\present\utils\arrays\ArrayUtils;

$arrayUtils = ArrayUtils::from(range(1,5));

//Squared all elements
$arrayUtils->mapAssoc(function($num){ return ["$num * $num", $num * $num]; });
// expected output: [
//  "1 * 1"   => 1,
//  "2 * 2"   => 4,
//  "3 * 3"   => 9,
//  "4 * 4"   => 16,
//  "5 * 5"   => 25
//]
```

{% endcode %}

## Syntax

```php
$arrayUtils->mapAssoc(callable $callback) : ArrayUtils;
```

### Parameter

* `$callback`

  > A function that produces an element of the new Array, taking three arguments:
  >
  > * `$value` The current element being processed in the array.
  > * `$key` The index of the current element being processed in the array.
  > * `$array`  The array `every` was called upon.

###

### Return value

* A new array with each element being the result of the callback function.

## Prefixing

```php
$arrayUtils->mapAssocAs(callable $callback) : array;
```

```php
ArrayUtils::mapAssocFrom(iterable $from, callable $callback) : ArrayUtils;
```

```php
ArrayUtils::mapAssocFromAs(iterable $from, callable $callback) : array;
```

## References

{% content-ref url="/pages/-MKmSpTD\_xJWCBfX5Mod" %}
[map()](/methods/c/map.md)
{% endcontent-ref %}


---

# Agent Instructions: 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:

```
GET https://arrayutils.docs.present.kim/methods/c/map/assoc.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
