Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to query for the existence of a value in an array by php

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "php how to query the array whether there is a certain value", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "php how to query the array whether there is a certain value" bar!

Query method: 1, with in_array (), you can search the array whether there is a specified value, syntax "in_array (" value ", array)"; 2, with array_search (), syntax "array_search (" value ", array)", if the value exists, return the corresponding key name, otherwise return false.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Php queries whether a certain value exists in the array

Method 1: use the in_array () function

In php, if you want to query whether an array has a certain value, you can directly use the built-in function in_array ()

The in_array () function searches for the existence of the specified value in the array. Syntax format:

The in_array ($search, $array, $strict) parameter is required to describe search. Specifies the value to search in the array. Array is required. Specifies the array to search. Strict is optional. If this parameter is set to TRUE, the in_array () function checks to see if the data searched is of the same type as the value of the array.

Return value: returns TRUE if a value is found in the array, FALSE otherwise.

Example:

Method 2: use the array_search () function

The array_search () function searches the array for a key value and returns the corresponding key name.

Using this function, you can also query whether a value exists in the array, return the corresponding key name if it does, and return false if it does not exist.

Syntax:

The array_search (value,array,strict) parameter is required to describe value. Specifies the key value to search in the array. Array is required. Specifies the array to be searched. Strict is optional. If this parameter is set to TRUE, the function searches the array for elements with the same data type and value. Possible values:

True

False-default

If set to true, the type of the given value is checked in the array, and the number 5 is different from the string 5 (see example 2).

Return value:

If the specified key value is found in the array, the corresponding key name is returned, otherwise FALSE is returned. If the key value is found more than once in the array, the key name that matches the first found key value is returned.

Example:

Thank you for your reading, the above is the content of "php how to query the array whether there is a certain value". After the study of this article, I believe you have a deeper understanding of the problem of how to query the array of php whether there is a certain value, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report