26

I'm trying to get all my first characters in a PHP array to be uppercase.

PHP code:

<?php
$ordlista = file_get_contents('C:/wamp/www/bilder/filmlista.txt');

$ord = explode("\n", $ordlista);

sort($ord,SORT_STRING);

foreach ($ord as $key => $val) {
    echo $val."<br/>";
}
?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Victor Bjelkholm
  • 2,177
  • 9
  • 28
  • 50
  • We don't have any sample data. Is it just one word per line in the file? Any multibyte characters? Are the words consistently all lowercase? – mickmackusa Aug 13 '21 at 15:20

4 Answers4

65
$ord = array_map('ucfirst', $ord);
iroel
  • 1,760
  • 1
  • 18
  • 27
  • or using anonymous function like this:
    $ord = array_map(create_function('$value', 'return ucfirst($value);'), $ord);
    – iroel Jul 23 '10 at 05:32
21
$ord=array_map(function($word) { return ucfirst($word); }, $ord);
icktoofay
  • 126,289
  • 21
  • 250
  • 231
  • Does'nt work... Notice: Use of undefined constant ucwords - assumed 'ucwords' in C:\wamp\www\sorting\sort.php on line 5 Warning: array_map() expects parameter 1 to be a valid callback, array must have exactly two members in C:\wamp\www\sorting\sort.php on line 5 Warning: sort() expects parameter 1 to be array, null given in C:\wamp\www\sorting\sort.php on line 7 Warning: Invalid argument supplied for foreach() in C:\wamp\www\sorting\sort.php on line 9 – Victor Bjelkholm Jul 23 '10 at 04:59
  • 1
    @Victor: Try now. For some reason it doesn't like `ucwords` as a callback, and it has to be wrapped in a closure. – icktoofay Jul 23 '10 at 05:01
  • @icktoofay That's not really a closure, that's an anonymous function. And functions in PHP are not first-class, so `array_map(ucfirst)` won't work, the correct syntax is `array_map('ucfirst')`. – deceze Jul 23 '10 at 05:12
  • 1
    @deceze: I was under the impression that it was a closure because if you run this in the PHP CLI interpreter: ``, you get an error saying `Object of class Closure could not be converted to string`. That, to me, says that it's a closure. – icktoofay Jul 23 '10 at 05:17
  • 1
    As the [manual states](http://www.php.net/manual/en/functions.anonymous.php): `Anonymous functions are currently implemented using the Closure class. This is an implementation detail.` It would be a closure if it *closed over* variables of the outer scope (using `use`), which it doesn't do. I guess the Closure class is used for any anonymous function because it's a subset of a closure. It's a fine distinction, admittedly. [Wikipedia even mentions](http://en.wikipedia.org/wiki/Closure_(computer_science%29): `The term closure is often mistakenly used to mean anonymous function.` – deceze Jul 23 '10 at 05:22
3

To support UTF-8 multibyte characters, like "Russian" for example, you would need

$ord = array_map(function($str){
    return mb_strtoupper(mb_substr($str, 0, 1)).mb_strtolower(mb_substr($str, 1));
}, $ord);

This uses the mb_ucfirst function from https://stackoverflow.com/a/14161325/175071

Community
  • 1
  • 1
Timo Huovinen
  • 53,325
  • 33
  • 152
  • 143
0

Sometimes, the raw data would looks something like this:

$ord = ['apple', 'GUAVA', 'mango', 'BANANA'];

To full-proof this:

$ord = array_map('ucfirst', array_map('strtolower', $ord));

What this does is it converts everything into a lower case first, then capitalize each word.

kapitan
  • 2,008
  • 3
  • 20
  • 26