Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get the key name in the php array

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "how to get the key names in the php array", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to get the key name in the php array.

Array_keys function-returns all the key names in the array

In PHP, you can use the array_keys function to get all the key names in the array by returning an array. The basic syntax format of the array_keys function is as follows:

Array_keys (array,value,strict)

Among them, it should be noted that:

Parameter array is a required parameter, which represents an array that needs to obtain key names. Parameter value is an optional parameter. When this parameter is left empty, the returned result is the key names of all values in the array. When the parameter value is specified, the returned result is the key name corresponding to the same array value of the parameter value.

The parameter strict is usually used with the parameter value. When the parameter strict is true, the key type in the returned result will be distinguished, and the string is different from the numeric value. When the parameter strict is false, this is also the default parameter of the parameter. The returned result indicates that the type will not be distinguished, and the string is the same as the numeric value.

Next, let's take a look at the use of the array_keys function through an example, as follows:

When the value parameter is not used

Output result:

The parameter value is not entered in the above example and the parameter strict,array_keys function returns all the key names of the array. Let's take a look at the difference between the output result after entering the parameter value. The example is as follows:

Output result:

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

Output result:

In the above example, although the key name of the string "222" is to be returned, when the parameter strict is set to true, the boundary between the string and the number is very strict, so only the key name of the string is output; when the parameter strict is set to false, the type of string and number is not strictly distinguished, 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 look at how to convert keys in an array to uppercase and lowercase.

Array_change_key_case function-convert key name case

In PHP, you can convert all key names in the array to uppercase or lowercase through the array_change_key_case function. The basic syntax format of the array_change_key_case function is as follows:

Array_change_key_case (array,case)

It should be noted that the parameter array represents the array that needs to be converted to uppercase and lowercase, and the parameter case is an optional parameter. By default, the value of the parameter CASE_LOWER represents the conversion of all key names in the array to lowercase, and when the parameter case is CASE_UPPER, it represents the conversion of all key names in the array to uppercase letters.

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

Let's take a look at the use of the array_change_key_case function through an example, as follows:

Output result:

In the above example, the parameter case is set to CASE_LOWER, so the key name in the array is lowercase, let's take a look at

What happens when the element key names in the array are both uppercase and lowercase, that is, when converted to uppercase or lowercase by the array_change_key_case function, the two keys are equal?

Let's take a look at the example, which is as follows:

Output result:

As you can see from the above example, when the two key names are equal after the conversion, the latter value will overwrite the previous value.

At this point, I believe you have a deeper understanding of "how to get the key names in the php array". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report