Skip to content

Instantly share code, notes, and snippets.

@haakym
Created December 19, 2017 21:09
Show Gist options
  • Save haakym/6eed1d08d8751b4a1f3bf77d9ead2b46 to your computer and use it in GitHub Desktop.
Save haakym/6eed1d08d8751b4a1f3bf77d9ead2b46 to your computer and use it in GitHub Desktop.
<?php
function array_build($array, $filter) {
foreach($array as $key => $value) {
unset($array[$key]);
$filter($key, $value);
$array[$key] = $value;
}
return $array;
}
// Example
// filter closure
$readableFilter = function(&$key, &$value){
$key = strtoupper($key);
$value = ucwords($value);
};
// test array of country and country code
$countriesArray = [
'us' => 'united states',
'uk' => 'united kingdom',
'in' => 'india',
];
// run array_build function
array_build($countriesArray, $readableFilter);
// output
/*
array(3) {
["US"]=>
string(13) "United States"
["UK"]=>
string(14) "United Kingdom"
["IN"]=>
string(5) "India"
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment