Home > Backend Development > PHP Problem > php query content position in array

php query content position in array

PHPz
Release: 2023-05-05 22:59:08
Original
1318 people have browsed it

In PHP, sometimes we need to find the position of a specific element in an array. This requirement is often encountered in programming. Because in actual business scenarios, we need to find a specific element from an array and then perform other operations.

So in PHP, how do we query the position of an element in an array? Below we will introduce several implementation methods in detail.

Method 1: Use the in_array() function to query

The in_array() function can query whether a value exists in an array. This function returns true if present, false otherwise. If we only need to know whether a certain value exists in the array and don't care about its specific position in the array, then we can use the in_array() function directly.

Sample code:

$arr = array("apple", "banana", "orange");
if (in_array("banana", $arr)) {
  echo "banana exists in the array";
} else {
  echo "banana does not exist in the array";
}
Copy after login

Output result:

banana exists in the array
Copy after login

Of course, we can also query the position of the element in the array through the in_array() function:

$arr = array("apple", "banana", "orange");
$position = array_search("banana", $arr);
if ($position !== false) {
  echo "banana exists in the array at position " . $position;
} else {
  echo "banana does not exist in the array";
}
Copy after login
Copy after login

Output result:

banana exists in the array at position 1
Copy after login
Copy after login

It should be noted that if the element does not exist in the array, the array_search() function will return false. Therefore, when using this function, be sure to perform type checking to avoid errors.

Method 2: Use the array_search() function to query

The array_search() function can find the position of an element in the array and return the key name of the element in the array. If the element does not exist in the array, the function returns false.

Sample code:

$arr = array("apple", "banana", "orange");
$position = array_search("banana", $arr);
if ($position !== false) {
  echo "banana exists in the array at position " . $position;
} else {
  echo "banana does not exist in the array";
}
Copy after login
Copy after login

Output result:

banana exists in the array at position 1
Copy after login
Copy after login

It should be noted that the array_search() function only returns the position of the first match of the element in the array . If the element appears multiple times in the array, the function only returns the first position of the element in the array.

Method 3: Use the array_keys() function to query

The array_keys() function can return all key names in the array that are equal to the specified value, and these key names form a new array. If the given value does not exist in the array, the function returns an empty array.

Sample code:

$arr = array("apple", "banana", "orange", "banana");
$positions = array_keys($arr, "banana");
if (count($positions) > 0) {
  echo "banana exists in the array at positions " . implode(", ", $positions);
} else {
  echo "banana does not exist in the array";
}
Copy after login

Output result:

banana exists in the array at positions 1, 3
Copy after login

It should be noted that the array_keys() function will return all key names in the array that are equal to the given value, so If the element appears multiple times in the array, the function returns multiple positions.

Summary:

The above are three ways to query the position of an element in an array in PHP. Which one to choose depends on the actual situation. If you only need to know whether a certain value exists in the array, use the in_array() function; if you need to query the position of the element in the array, you can use the array_search() function; if you need to query all matches of the element in the array location, you can use the array_keys() function.

The above is the detailed content of php query content position in array. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template