search
HomeBackend DevelopmentPHP TutorialPHP array operation matching search elements and key names in the array

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
PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP: The Foundation of Many WebsitesPHP: The Foundation of Many WebsitesApr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

Beyond the Hype: Assessing PHP's Role TodayBeyond the Hype: Assessing PHP's Role TodayApr 12, 2025 am 12:17 AM

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

What are Weak References in PHP and when are they useful?What are Weak References in PHP and when are they useful?Apr 12, 2025 am 12:13 AM

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

Explain the __invoke magic method in PHP.Explain the __invoke magic method in PHP.Apr 12, 2025 am 12:07 AM

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.