Home  >  Article  >  Backend Development  >  PHP query array element subscript

PHP query array element subscript

王林
王林Original
2023-05-05 21:05:101387browse

In PHP, sometimes we need to query the subscript position of an element in an array. This situation is very common in programming. In this article, we will introduce several methods to query the subscript position of an element in an array.

1. Use the array_search function

PHP provides a built-in function array_search, which can search for a specified value in an array and return the key name corresponding to the value (that is, the following mark). The array_search function is similar to in_array, except that it returns the element index instead of true/false.

The following is a simple example:

$fruits = ['apple', 'banana', 'orange'];

$key = array_search('banana', $fruits); // 返回 1

echo $key;

In the above example, we use the array_search function to find the subscript position of the element with the value 'banana' in the array $fruits. Since the index of 'banana' in the array is 1, the function returns a value of 1.

It should be noted that if the element does not exist in the array, the array_search function will return false. Therefore, judgment is required when using this function.

2. Use the array_keys function

In addition to using the array_search function, we can also use the array_keys function to query the subscript position of an element in the array. This function will return an array containing the index of the specified element.

The following is an example:

$fruits = ['apple', 'banana', 'orange'];

$keys = array_keys($fruits, 'banana');

print_r($keys); // 输出 [1]

In the above example, we use the array_keys function to query the subscript position of the element with the value 'banana' in the array $fruits. Since the index of this element in the array is 1, the value returned by this function is an array containing the element with value 1.

It should be noted that if the element does not exist in the array, the array_keys function will return an empty array. Therefore, judgment is required when using this function.

3. Use the foreach loop

In addition to using the built-in function, we can also use the foreach loop to traverse the array to query the subscript position of an element in the array. Here is an example:

$fruits = ['apple', 'banana', 'orange'];

foreach ($fruits as $key => $value) {
    if ($value === 'banana') {
        echo $key; // 输出 1
        break;
    }
}

In the above example, we use a foreach loop to iterate through the $fruits array and use a conditional statement to determine if an element with the value 'banana' is found. If found, we output the corresponding index position and use the break statement to terminate the loop.

It should be noted that when using the foreach loop to traverse the array, once the specified element is found, we need to use the break statement to jump out of the loop, otherwise the entire array will continue to be traversed.

4. Use the array_flip function

When we need to frequently query the subscript position of an element in the array, we can use the array_flip function to swap the subscript and value of the array, so that we can query an element. The subscript position of an element can be turned into the corresponding subscript by querying the element value. The example is as follows:

$fruits = ['apple', 'banana', 'orange'];
$fruits = array_flip($fruits);

$key = $fruits['banana']; // 返回 1

echo $key;

In the above example, we use the array_flip function to swap the subscripts and values ​​​​of the $fruits array, and then use the array subscript to query the position of the element with the value 'banana' in the array. mark location. Since the index of 'banana' in the original array is 1, the value returned by this function is also 1.

It should be noted that the array_flip function will retain one element with the same value, so if there are multiple identical elements in the original array, these elements will only appear once in the reversed array.

Summary:

In PHP, it is very common to query the subscript position of an element in an array. Commonly used methods include array_search, array_keys, foreach loop, array_flip function, etc. It is necessary to choose the most appropriate method according to the actual situation.

The above is the detailed content of PHP query array element subscript. 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