Get the App
SLTechnology News&Howtos  ›  Development  › 

Whether the key name of the array in php can be repeated.

Shulou Source: shulou.com Published: 2022-05-31 18:49:14 10月18日 Update

Today, the editor will share with you the relevant knowledge points about whether the key names of the arrays in php can be repeated. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

The key name of the php array cannot be duplicated. In php, the key of the array is unique and will not repeat. Even if two identical key names are declared, the key name of the latter declaration will overwrite the previous key name; using this feature, you can use array_flip () to reverse the key name and key value of the array twice to achieve the array deduplication effect.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The key name of the php array cannot be duplicated.

In php, the key name (key) of the array is unique and does not repeat.

Example: there is such an empty array

$arr = array (); / / define an empty array var_dump ($arr); / / output array

Declare key-value pairs

/ / define key value pair element $arr ["id"] = 1 * * name] = "Li Hua"; $arr ["age"] = 20 * dump ($arr); / / output array

Declare duplicate key names and assign values

/ / declare duplicate key names and assign a value of $arr ["id"] = 10 positionarr ["age"] = 23 transfixion dump ($arr); / / output array

You can see that even if two identical key names are declared, the key name of the latter declaration will overwrite the previous key name.

Extended knowledge: taking advantage of the non-repeatable key name of php array, the repeated values of the array can be removed.

Just use the array_flip ($array) function to reverse the key name and key value of the array twice:

First swap the key name and key value to remove the duplicate value

And then switch the key name and key value back.

These are all the contents of the article "whether the key names of arrays in php can be repeated". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Array knowledge article output but not two content times characteristics different large elements functions most that is effects tutorials more environment computer Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info macOS Linux Shulou Technology