Home  >  Article  >  Backend Development  >  PHP array operation matching search elements and key names in the array

PHP array operation matching search elements and key names in the array

WBOY
WBOYOriginal
2021-11-05 10:39:274424browse

In the previous article "How to compare strings in PHP? (Detailed explanation of examples) "In "We have introduced in detail the relevant knowledge of how to compare two strings in PHP. In this article, we will take a look at the relevant knowledge of array operations in PHP. I hope it will be helpful to everyone. helpful!

PHP array operation matching search elements and key names in the array

In the previous article we learned that strings can be processed through the strcmp() function and the strcasecmp() function Comparison, string is one of the important data types in PHP. Another very important data type in the PHP development process is array. We have learned that strings can be compared, positioned, replaced, etc., so for the same Do we have any common operations on important arrays?

In the previous article "How to locate elements in strings and arrays in PHP? 》We talked about how to search and locate elements in strings and arrays. We mentioned that you can use the preg_grep() function to locate and search array elements. Let’s take a look at more method to find and search elements in an array.

<strong><span style="font-size: 20px;">in_array</span></strong>() function - matches array elements and returns Boolean

In PHP we can search for elements in the array through the in_array() function. The basic syntax format of the in_array() function is as follows:

in_array(search,array,type)

What needs to be noted is that the parameter search represents the value we need to search in the array, the parameter array represents the array we need to search, and the parameter type is an optional parameter. If the value of the parameter is true, during the search, it will be checked whether the searched data and the searched array data type are the same.

If the value we need to search is found in the array, the result returned by the function is true; if the value we need to search is not found in the array, the result returned The result is false.

Next, let’s look at the application of the in_array() function in PHP through a simple example. The example is as follows:

<?php
$people = array("Bill", "bob", "Mark", "coc");
if (in_array("23", $people, TRUE))
  {
  echo "在数组中";
  }
else
  {
  echo "不在数组中";
  }
  echo &#39;<br/>&#39;;
if (in_array("Mark",$people, TRUE))
  {
  echo "在数组中";
  }
else
  {
  echo "不在数组中";
  }
  echo &#39;<br/>&#39;;
if (in_array("bill",$people, TRUE))
  {
  echo "在数组中";
  }
else
  {
  echo "不在数组中";
  }
?>

Output result:

PHP array operation matching search elements and key names in the array

In the above example, when we searched for the third time, we used lowercase and the match was not successful. What needs to be noted is that if the content we need to search is a string, and the parameter type is set to true, then the search will be case-sensitive.

<strong><span style="font-size: 20px;">array_search</span></strong>() function - matches array elements and returns the key name

The above in_array function knowledge simply searches to determine whether there are elements we need to find in the array. There is no way to locate them. If we want to accurately locate and find them, in PHP we You can use the array_search() function, which can search for elements, and the result returned is the key name of the element we are searching for. The basic syntax format of

array_search() function is as follows:

array_search(value,array,strict)

What needs to be noted is that the parameter value is what we need to search for Key value, parameter array is the array we need to search, parameter strict is an optional parameter, this parameter is flase by default, if the parameter is set If it is true, when searching, it will be checked whether the searched data and the searched array data type are the same.

If the corresponding key value is searched in the array, the returned result is the key name corresponding to the key value; if there is no match, the returned result is false; it needs to be noted that if If more than one key value is matched, the result returned at this time is the key name that matches the key value for the first time.

Let’s take a look at the use of the array_search function through an example. The example is as follows:

<?php
$a=array("a"=>"5","b"=>5,"c"=>"5");
echo array_search(5,$a,true) . &#39;<br/>&#39;;
$array = array(0 => &#39;blue&#39;, 1 => &#39;red&#39;, 2 => &#39;green&#39;, 3 => &#39;red&#39;);
echo array_search(&#39;green&#39;,$array,true);
?>

Output result:

PHP array operation matching search elements and key names in the array

It should be noted that when the parameter is set to true, when searching and matching the array, the search results will be different for different data forms.

<strong><span style="font-size: 20px;">array_key_exists</span></strong>##() function - matches array key name and returns Boolean

In the above, we can use the

array_search function to output the key name of the search element through the search key value. In PHP, we can also directly search the key name. That is through the array_key_exists function in PHP.

array_key_existsThe basic syntax format of the function is as follows:

array_key_exists(key,array)

其中需要注意的是:参数key表示的就是我们需要所搜的键名,参数array标识的就是我们需要进行搜索的数组,

通过array_key_exists函数只能够判断一维数组中的键名不能判断多维数组中数组内的键名,如果在数组中匹配到了指定的键名,该函数返回的结果就是true,如果数组中没有匹配到。返回的结果就是flase。

下面我们通过示例来看一下array_key_exists函数的使用,示例如下:

<?php
$people = array("Bill", "a"=>"bob", "Mark", "coc");
if (array_key_exists(0, $people,))
  {
  echo "键名存在";
  }
else
  {
  echo "键名不存在";
  }
  echo &#39;<br/>&#39;;
if (array_key_exists("a",$people,))
  {
  echo "键名存在";
  }
else
  {
  echo "键名不存在";
  }
  echo &#39;<br/>&#39;;
if (array_key_exists("coc",$people,))
  {
  echo "键名存在";
  }
else
  {
  echo "键名不存在";
  }
?>

输出结果:

PHP array operation matching search elements and key names in the array

由此我们便通过array_key_exists来进行在一个数组中找到一个指定的键。

大家如果感兴趣的话,可以点击《PHP视频教程》进行更多关于PHP知识的学习。

The above is the detailed content of PHP array operation matching search elements and key names in the 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