Home  >  Article  >  Backend Development  >  How to get array key in php

How to get array key in php

PHPz
PHPzOriginal
2023-04-18 09:06:114163browse

PHP is a scripting language widely used for web development. It has many uses, one of which is processing data. In PHP, an array is a very common data structure that can store multiple values. When operating an array, in many cases it is necessary to obtain the key of the array. In this article, we will introduce ways to get array keys in PHP.

1. Use foreach loop

The foreach loop is one of the most commonly used methods of traversing arrays in PHP. This way we can easily iterate through each element in the array and get their keys and values.

The following is a basic example:

$fruits = array("apple" => "苹果", "orange" => "橙子", "grape" => "葡萄");

foreach ($fruits as $key => $value) {
  echo $key . ":" . $value . "<br>";
}

In the above example, we define an associative array named $fruits, containing three key-value pairs. Then use a foreach loop to iterate through the array and assign the key and value of each element to the variables $key and $value respectively. During the loop, we output the key and value of each element through the echo statement.

2. Use the array_keys function

The array_keys function is a function in PHP used to obtain all keys in an array. It can return a new array consisting of all keys in the array.

The following is an example:

$fruits = array("apple" => "苹果", "orange" => "橙子", "grape" => "葡萄");
$keys = array_keys($fruits);

print_r($keys);

In the above example, we first define an associative array $fruits. Then use the array_keys function to get all the keys in the array and store the keys in a new array $keys. Finally, use the print_r function to output the $keys array.

3. Use the array_key_exists function

array_key_exists is a function in PHP used to check whether the specified key exists in the array. This function returns true if the key exists, false otherwise.

Here is an example:

$fruits = array("apple" => "苹果", "orange" => "橙子", "grape" => "葡萄");

if (array_key_exists("orange", $fruits)) {
  echo "存在";
} else {
  echo "不存在";
}

In the above example, we check if the key "orange" exists in the $fruits array. If it exists, we will output "exists", otherwise we will output "does not exist".

4. Use the key function

The key function is a function in PHP used to obtain the key of the element pointed to by the current pointer of the array. When we traverse an array using a foreach loop or the current function, we can use the key function to get the key of the current element.

The following is an example:

$fruits = array("apple" => "苹果", "orange" => "橙子", "grape" => "葡萄");

foreach ($fruits as $key => $value) {
  echo key($fruits) . "<br>";
  next($fruits);
}

In the above example, we use a foreach loop to traverse the $fruits array, and use the key function in the loop body to get the key of the current element. Since we also use the next function in the loop body, the pointer will move backward after each loop, allowing the key function to obtain the key of the next element.

Summary

There are many ways to get the keys of an array in PHP, including using the foreach loop, array_keys function, array_key_exists function and key function. Which method to choose depends on the specific scenario and needs. Among them, the foreach loop is the most common method of obtaining array keys, and it is also one of the most flexible and powerful methods.

The above is the detailed content of How to get array key in php. 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