Christian Mayer's Weblog

PHP: array_map_assoc()

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);

Posted on .
Categories: Programming, Productivity
Tags: PHP, Array, assoc, associative, associatively, Map, Development

Imprint | Usage | Categories | RSS Feed
Copyright © 2006 by