I'm looking for a nifty php solution to replace a standard forloop. i dont care at all about the inner workings of my normalize method.
Here is my code:
$pairs=[];
foreach($items as $key => $val){
    $key = $this->normalizeKey($key);
    $pairs[$key] = $val;
}
private function normalizeKey($key)
{
    // lowercase string
    $key = strtolower($key);
    // return normalized key
    return $key;
}   
I'd like to learn the PHP language a bit better. I thought array_walk would be nice to use, but that operates on the values and not the keys. 
I am looking for a PHP array method that can perform this code instead of a foreach loop.
You want array_change_key_case
$pairs = array_change_key_case($items);
                        Just for fun, though it requires three functions.  You can replace strtolower() with whatever since this can already be done with array_change_key_case():
$pairs = array_combine(array_map('strtolower', array_keys($items)), $items);
$itemsstrtolower()$items valuesYou can also use an anonymous function as the callback if you need something more complex.  This will append -something to each key:
$pairs = array_combine(array_map(function ($v) {
                                     return $v . '-something';
                                 },
                       array_keys($items)), $items);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With