Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
PHP helper to recursive iterate over an array. Method will send `$value` and `$key` to callback. `$values_only` option allow to send only final values to callback.
<?php declare(strict_types=1);
* @param array $array
* @param callable $callback
* @param bool $values_only = true
* @return array
public function arrayMapRecursive(array $array, callable $callback, bool $values_only = true): array
$map = function ($value, $key) use ($callback, $values_only) {
if (is_array($value) === false) {
return $callback($value, $key);
if ($values_only) {
return $this->arrayMapRecursive($value, $callback, $values_only);
return $this->arrayMapRecursive($callback($value, $key), $callback, $values_only);
return array_combine($keys = array_keys($array), array_map($map, $array, $keys));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment