Home > Backend Development > PHP Tutorial > How to get the key name in the array? How to convert the case of key names?

How to get the key name in the array? How to convert the case of key names?

WBOY
Release: 2023-04-10 19:36:01
Original
3646 people have browsed it

In the previous article "How to fill arrays in different ways in PHP? " provides a detailed introduction to the relevant knowledge of how to fill arrays in PHP. In this article, we will take a look at the related operations of key names in PHP array operations. I hope it will be helpful to everyone!

How to get the key name in the array? How to convert the case of key names?

In the previous article, we learned that the array filling operation can be realized through the array_fill function and the array_fill_keys function. Let's take a look at how to output all the key names in the array in PHP. How can I convert all key names in an array to uppercase or lowercase?

If you want to get all the key names in the array, you can use the array_keys function. If you want to convert all the key names in the array to uppercase or lowercase, you can use the array_change_key_case function. To implement, let’s take a look at how to use these two functions.

<span style="font-size: 20px;">array_keys</span>Function-returns all key names in the array

Can be done in PHP Use the array_keys function to get all the key names in the array by returning an array form. The basic syntax format of the array_keys function is as follows:

array_keys(array,value,strict)
Copy after login

What you need to pay attention to Yes:

Parameterarray is a required parameter, which represents the array that needs to obtain the key name; parametervalue is an optional Select a parameter. When this parameter is not filled in, the returned result is the key name of all the values ​​in the array. When the parameter value is specified, the returned result is the same as the parameter value. The key name corresponding to the array value.

Parameter strict is usually used together with parameter value. When parameter strict is true, it means return The key name types in the results will be distinguished. Strings and values ​​are different. When the parameter strict is false, this is also the default parameter of the parameter. The returned result is clearly There is no difference in type, strings and numbers are the same.

Next let’s look at the use of the array_keys function through an example. The example is as follows:

When the value parameter is not used

<?php
$a=array("aaa"=>"好好学习","bbb"=>"天天向上","ccc"=>"福如东海","ddd"=>"寿比南山");
print_r(array_keys($a));
?>
Copy after login

Output result:

How to get the key name in the array? How to convert the case of key names?

In the above example, the parameter value and parameter strict are not filled in, and the array_keys function returns all the keys of the array. name, let’s take a look at the difference in the output results after inputting the parameter value. The example is as follows:

<?php
$a=array("aaa"=>"好好学习","bbb"=>"天天向上","ccc"=>"福如东海","ddd"=>"寿比南山");
print_r(array_keys($a));
echo &#39;<br/>&#39;;
print_r(array_keys($a,"天天向上"));
?>
Copy after login

Output results:

How to get the key name in the array? How to convert the case of key names?

In the above example, after the parameter value is set, the returned result will only be the key name corresponding to the parameter value. Let's take a look at the difference in the output results if the parameter strict is different. The example is as follows:

<?php
$a=array("aaa"=>"111","bbb"=>"222","ccc"=>111,"ddd"=>222);
print_r(array_keys($a,"222",true));
echo &#39;<br/>&#39;;
print_r(array_keys($a,"222",false));
?>
Copy after login

Output Result:

How to get the key name in the array? How to convert the case of key names?

#In the above example, although the key name of the string "222" is returned, when the parameter strict is set to true, the string 222 and The limit of the number 222 is very strict, so only the key name of the string 222 is output; when the parameter strict is set to false, the type distinction between strings and numbers is not strict, so the output result will have two key names.

The above example is to output the key names in the array through the array_keys function. Let's take a look at how to convert the keys in the array to uppercase and lowercase.

<strong><span style="max-width:90%">array_change_key_case</span></strong>##Function - Convert key name to uppercase or lowercase

In PHP, you can use the

array_change_key_case function to convert all key names in the array to uppercase or lowercase. The basic syntax format of the array_change_key_case function is as follows:

array_change_key_case(array,case);
Copy after login

You need to pay attention to this The answer is: Parameter

array represents the array that needs to be converted from upper to lower case. Parameter case is an optional parameter. By default, the value of the parameter is CASE_LOWER. It means converting all the key names in the array to lowercase. When the parameter case is CASE_UPPER, it means converting all the key names in the array to uppercase letters.

The returned result is an array that has been converted to upper and lower case. If there is an error in the array, the output result is false.


Let’s look at the use of the

array_change_key_case function through an example. The example is as follows:

<?php
$a=array("AAA"=>"111","BbB"=>"222","ccC"=>"333");
print_r(array_change_key_case($a,CASE_LOWER));
?>
Copy after login

Output result:

How to get the key name in the array? How to convert the case of key names?

上述示例中,参数case设置成了CASE_LOWER因此数组中的键名成了小写,下面我们来看一下,

当数组中的元素键名,既存在大写也存在小写的时候,也就是说,当通过array_change_key_case函数转换为大写或者小写的时候,两个键名会相等,这时候结果会有什么变化?

我们通过示例来看一下,示例如下:

<?php
$a=array("AAA"=>"111","BbB"=>"222","aaa"=>"333");
print_r(array_change_key_case($a,CASE_UPPER));
?>
Copy after login

输出结果:

How to get the key name in the array? How to convert the case of key names?

通过上述示例能够看出,当转换之后两个键名相等的时候,后面的值会将前面的值给覆盖掉。

大家如果感兴趣的话,可以点击《PHP视频教程》进行更多关于PHP知识的学习。

The above is the detailed content of How to get the key name in the array? How to convert the case of key names?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template