Home  >  Article  >  Backend Development  >  How to change all values ​​of PHP array to uppercase or lowercase

How to change all values ​​of PHP array to uppercase or lowercase

藏色散人
藏色散人Original
2021-08-10 17:58:193772browse

This article will introduce to you how to change all the values ​​​​in PHP arrays to uppercase or lowercase. I believe that after reading this article, you will have a better understanding of PHP arrays. I also hope that the problem-solving ideas involved in the article can It will help everyone use it in daily development~

First of all, I will give an example array $Color, as follows:

$Color = array('A' => 'Blue', 'B' => 'Green', 'c' => 'Red');

You can operate it locally and convert the value to Uppercase or lowercase.

The following is the implementation method I gave:

The complete PHP code is as follows:

 $value)
    {
        if (is_array($value))
        {
            $narray[$key] = array_change_value_case($value, $case);
            continue;
        }
        $narray[$key] = ($case == CASE_UPPER ? strtoupper($value) : strtolower($value));
    }
    return $narray;
}
$Color = array('A' => 'Blue', 'B' => 'Green', 'c' => 'Red');
echo '原始数组:';
var_dump($Color);
echo '值是小写的:';
$myColor = array_change_value_case($Color,CASE_LOWER);
var_dump($myColor);
echo '值是大写的:';
$myColor = array_change_value_case($Color,CASE_UPPER);
var_dump($myColor);

The results are as follows:

How to change all values ​​of PHP array to uppercase or lowercase

Isn’t it also very simple!

Here you need to know three functions:

1, is_array() Function: detect whether the variable is an array, the syntax is "is_array(mixed $var): bool", if var is an array, returns true, otherwise returns false.

2, strtoupper() Function: used to convert a string to uppercase. This function is binary safe, and its return value is the string converted to uppercase.

3, strtolower() Function: used to convert a string to lowercase. This function is binary safe, and its return value is the string converted to lowercase.

Attached related functions:

lcfirst(): Convert the first character in the string to lowercase

strtolower(): Convert the string To lowercase

ucfirst(): Convert the first character in the string to uppercase

ucwords(): Convert the first character of each word in the string to uppercase

Finally, I would like to recommend the latest and most comprehensive "PHP Video Tutorial" to everyone~ Come and learn!

The above is the detailed content of How to change all values ​​of PHP array to uppercase or 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