


How to get the key name in the array? How to convert the case of key names?
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!
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)
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)); ?>
Output result:
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 '<br/>'; print_r(array_keys($a,"天天向上")); ?>
Output results:
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 '<br/>'; print_r(array_keys($a,"222",false)); ?>
Output Result:
#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
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);
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.
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)); ?>
上述示例中,参数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)); ?>
输出结果:
通过上述示例能够看出,当转换之后两个键名相等的时候,后面的值会将前面的值给覆盖掉。
大家如果感兴趣的话,可以点击《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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
