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 ) 

demo


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -