Home>Article>Backend Development> Can the php array key name be omitted?

Can the php array key name be omitted?

WBOY
WBOY Original
2022-09-20 16:48:51 1625browse

The key name of the PHP array cannot be omitted; the characteristic of the PHP array is to map values to the type of keys. An element value must correspond to a key name. You can use the "array_values()" function to remove the specified keys in the array. Key name, returns an array containing all the values in the array, and uses numeric keys starting from 0 and increasing by 1.

Can the php array key name be omitted?

The operating environment of this article: Windows 10 system, PHP version 8.1, Dell G3 computer

Can the php array key name be omitted?

The key name of the PHP array cannot be omitted

The characteristic of the PHP array is to map values to the type of keys

The key of the array in PHP can be a string, and values can be of any type.

You can use the array_values() function to remove all key names in the array and retain the key values. This function can return an array containing all key values in the array.

The returned array will use numerical values. Key, starting from 0 and increasing by 1.

The syntax is:

array_values(array)

The example is as follows:

"Peter","Age"=>"41","Country"=>"USA"); print_r(array_values($a)); ?>

Output result:

Can the php array key name be omitted?

Or use " array_keys($array)" statement can remove all key values and return an array containing all key names

array_keys() function returns a new array containing all key names in the array.

Grammar

array_keys(array,value,strict)

The example is as follows:

"XC90","BMW"=>"X5","Toyota"=>"Highlander"); print_r(array_keys($a)); ?>

Output result:

Can the php array key name be omitted?

Recommended learning:《PHP Video tutorial

The above is the detailed content of Can the php array key name be omitted?. 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