Home > Backend Development > PHP Problem > How to determine whether there is a certain dimension array in a two-dimensional array in PHP

How to determine whether there is a certain dimension array in a two-dimensional array in PHP

PHPz
Release: 2023-04-12 11:29:59
Original
723 people have browsed it

In PHP, processing multi-dimensional arrays is one of the common tasks. For a two-dimensional array, we sometimes need to find a one-dimensional array containing a specific element. So how to achieve it?

Method 1: Use the in_array function

PHP’s built-in in_array function can find whether a specific element is contained in a one-dimensional array. So for a two-dimensional array, we can loop through the one-dimensional array and call the in_array function to search respectively. If found, it will return true, otherwise it will return false.

The following is the sample code:

function searchSubArrayInArray($needle, $haystack) {
  foreach ($haystack as $subArray) {
    if (in_array($needle, $subArray)) {
      return true;
    }
  }
  return false;
}
Copy after login

In the above example, $needle represents the one-dimensional array to be searched, and $haystack represents the two-dimensional array to be searched. This function returns a bool type value indicating whether it exists.

It should be noted that the in_array function is case-sensitive, so you need to pay attention to the case when using it.

Method 2: Use the array_search function

In addition to the in_array function, PHP also provides another way to find elements: the array_search function. This function searches an array for a given value and, if found, returns the key for that value (i.e. a one-dimensional array). Returns false if not found.

The following is the sample code:

function searchSubArrayInArray($needle, $haystack) {
  foreach ($haystack as $key => $subArray) {
    if (array_search($needle, $subArray) !== false) {
      return $key;
    }
  }
  return false;
}
Copy after login

In the above example, $needle represents the one-dimensional array to be searched, and $haystack represents the two-dimensional array to be searched. This function returns an int type value (that is, the key of the one-dimensional array containing needle in the two-dimensional array), or false.

It should be noted that if multiple one-dimensional arrays contain needle elements, the array_search function will only return the first matching key.

Summary

The above are two common methods to determine whether a two-dimensional array contains a one-dimensional array. If it needs to be used in actual projects, different methods need to be selected according to the actual situation.

The above is the detailed content of How to determine whether there is a certain dimension array in a two-dimensional array in PHP. 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