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

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

Nov 05, 2021 am 10:39 AM
php 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)
Copy after login

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 "不在数组中";
  }
?>
Copy after login

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="max-width:90%">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)
Copy after login

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);
?>
Copy after login

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="max-width:90%">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)
Copy after login

其中需要注意的是:参数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 "键名不存在";
  }
?>
Copy after login

输出结果:

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 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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1267
29
C# Tutorial
1239
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 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

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 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.

See all articles