Home  >  Article  >  Backend Development  >  Convert first letter of php array from uppercase to lowercase

Convert first letter of php array from uppercase to lowercase

王林
王林Original
2023-05-06 12:14:07596browse

In PHP, using arrays is a very common task. We usually use various methods to operate the elements of the array. One of the common requirements is to convert the first letter of the elements in an array from uppercase to lowercase. Below we will introduce several ways to achieve this task.

Method 1: Use the foreach loop to traverse the array

First, we can use the foreach loop to traverse the array, and then use the PHP built-in function ucfirst() to convert the first letter of each element to uppercase. Then we use the strtolower() function to convert the letter to lowercase.

The sample code is as follows:

$arr = array("Apple", "Banana", "Cherry", "Durian");

foreach ($arr as $key => $value) {
    $arr[$key] = strtolower(ucfirst($value));
}

print_r($arr);

In this example, first we define an array containing several words. Then use a foreach loop to iterate through the array, use the ucfirst() function to convert the first letter of each array element to uppercase, and then use the strtolower() function to convert the letter to lowercase. Finally, use the print_r() function to print out the converted array.

The output results are as follows:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => durian
)

Method 2: Use array_map() function and strtolower() function

Another implementation method is to use array_map() function and strtolower( ) functions and use them in combination.

The sample code is as follows:

$arr = array("Apple", "Banana", "Cherry", "Durian");

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

print_r($arr);

In this example, first we also define an array containing several words. We then use the array_map() function to pass each element of the array to the strtolower() function and ucfirst() function respectively, and use their combined result as the element of the new array. Finally, use the print_r() function to print out the converted array.

The output result is the same as method one:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => durian
)

Method three: use array_walk() function

The last method is to use array_walk() function to traverse the array, and then call back function to achieve conversion.

The sample code is as follows:

$arr = array("Apple", "Banana", "Cherry", "Durian");

array_walk($arr, function(&$value) {
    $value = strtolower(ucfirst($value));
});

print_r($arr);

In this example, we use the array_walk() function and pass in an anonymous function as the second parameter. In this anonymous function, we use the pass-by-reference method to modify the value of the array element and convert it from uppercase to lowercase. Finally, use the print_r() function to print out the converted array.

The output results are the same as the first two methods:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => durian
)

Summary

Above we have introduced three common methods of converting the first letter of the elements in the array from uppercase to lowercase. . These methods are implemented using loops and PHP built-in functions, the array_map() function, and finally the array_walk() function. In practical applications, we can choose the appropriate method to operate according to actual needs.

The above is the detailed content of Convert first letter of php array from uppercase to lowercase. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn