php - Add string / symbol to all keys in array -
what easiest way ( without looping manually ) to, in case, prepend dollar sign all keys of associative array?
$input = array('fruit' => 'apple', 'car' => 'volvo');
expected output
array('$fruit' => 'apple', '$car' => 'volvo');
try snippet below
$input = array('fruit' => 'apple', 'car' => 'volvo'); $array = array_combine( array_map(function($k){ return '$' . $k; }, array_keys($input)), $input ); print_r($array);
output:
array ( [$fruit] => apple [$car] => volvo )
Comments
Post a Comment