manniru
5/4/2017 - 3:39 PM

Implode an associative array using `array_walk()`

Implode an associative array using array_walk()

<?php
$array = array(
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3',
);
$flattened = $array;
array_walk($flattened, function(&$value, $key) {
    $value = "{$key} ({$value})";
});
echo 'The values are ' . implode(', ', $flattened);