PHP: array_map_assoc()

Published in category Programming and Productivity
on Christian Mayer's Weblog.

With array_map() you only can walk through the keys of an given array. Why is there no array_map_assoc()? For walking through an associative array to map also the keys not only the values.

<?php
function array_map_assoc($func, $ar){
	$rv = array();
	foreach($ar as $key => $val){
		$func($key, $val);
		$rv[$key] = $val;
	}
	return $rv;
}

$assoc_array1 = array('x' => 'y', 'z' => 'abc');
$assoc_array2 = array_map_assoc(function(&$key, &$val){ $key = ':'.$key; }, $assoc_array1);
var_export($assoc_array2);

Recent Posts

About the Author

Christian is a professional software developer living in Vienna, Austria. He loves coffee and is strongly addicted to music. In his spare time he writes open source software. He is known for developing automatic data processing systems for Debian Linux.