Home  >  Article  >  Backend Development  >  How to check specified elements in php array

How to check specified elements in php array

PHPz
PHPzOriginal
2023-04-20 13:51:05416browse

PHP is a widely used server-side programming language. By using PHP arrays, we can easily store and manipulate data in applications. As one of the core functions of PHP, arrays are widely used to handle different types of data, such as numbers, strings, and objects. In this article, we will focus on how to find and return a specified element in a PHP array.

PHP's arrays have many available functions that can find and access array elements. Specifically, we can use the following function to find the specified element in the array:

  1. in_array() function

This function is used to find in the given array the specified value. It accepts two parameters, the first is the value to find and the second is an array. If the specified value is found, the function returns true; otherwise, it returns false.

For example, the following code finds whether an element with a value of 5 exists in the numeric array $nums:

$nums = array(2, 5, 8, 11, 14);
if (in_array(5, $nums)) {
  echo "已找到";
} else {
   echo "未找到";
}

The above code will output "Found" because $nums contains an element with a value of 5 element.

  1. array_search() function

The array_search() function is similar to the in_array() function, but it returns the key of the specified value. If the specified value does not exist in the array, the function returns false.

For example, the following code finds whether "peach" exists in the string array $fruits and returns its key value:

$fruits = array("西瓜", "苹果", "香蕉", "桃子");
$value = array_search("桃子", $fruits);
if ($value) {
  echo "已找到,键值为: " . $value;
} else {
  echo "未找到";
}

The above code will output "Found, the key value is: 3" because the key value of "peach" is 3.

  1. array_key_exists() function

array_key_exists() function is used to check whether the specified key name exists in an array. If it exists, the function returns true; otherwise, it returns false.

For example, the following code checks whether the key named "Tom" exists in the associative array $grades:

$grades = array("Tom"=>76, "Jane"=>85, "Bob"=>93);
if (array_key_exists("Tom", $grades)) {
  echo "已找到";
} else {
  echo "未找到";
}

The above code will output "Found" because the key named "Tom" exists in $grades Elements of "Tom".

  1. array_intersect() function

This function is used to return elements contained in two or more arrays. Specifically, it accepts multiple arrays as parameters and returns the elements that are common in these arrays.

For example, the following code returns the elements that co-exist in the arrays $nums1 and $nums2:

$nums1 = array(1, 3, 5, 7, 9);
$nums2 = array(2, 3, 5, 8, 11);
$result = array_intersect($nums1, $nums2);
print_r($result);

The above code will output the array array(3, 5), because both $nums1 and $nums2 contain Elements with values ​​3 and 5.

Summary:

In PHP, you can easily find and access array elements using these functions. In the actual application process, different functions can be selected for operation according to specific needs.

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