Voting

: seven minus three?
(Example: nine)

The Note You're Voting On

NetPanther
13 years ago
Use this to capitalize first letter of all array keys:

<?php

function ucfirstKeys(&$data)
{
  foreach ($data as $key => $value)
  {
    // Convert key
    $newKey = ucfirst($key);

    // Change key if needed
    if ($newKey != $key)
    {
      unset($data[$key]);
      $data[$newKey] = $value;
    }

    // Handle nested arrays
    if (is_array($value))
    {
      ucfirstKeys($data[$key]);
    }
  }
}

$test = array('foo' => 'bar', 'moreFoo' => array('more' => 'foo'));
ucfirstKeys($test);
print_r($test);

?>

Result:
Array ( [MoreFoo] => Array ( [More] => foo ) [Foo] => bar )

<< Back to user notes page

To Top