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 understand PHP in_array function

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "how to understand the PHP in_array function". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

However, if the array is relatively large, the performance will degrade and it will run longer. For optimization in the case of a large array, here are two methods (both of which are implemented through custom functions):

1. The array key and value are flipped to determine whether the key exists in the array by isset.

The copy code is as follows:

/ * *

* in_array is too slow when array is large

, /

Public static function inArray ($item, $array) {

$flipArray = array_flip ($array)

Return isset ($flipArray [$item])

}

You may also ask why array_key_exists is not used as a dual-use isset. Let's look at the comparison between array_key_exists () and isset ():

Isset () does not return TRUE for values that are NULL in the array, while array_key_exists () does.

The copy code is as follows:

two。 Connect with implode and judge directly with strpos

Connect with implode function + comma and judge directly with strpos. The speed of string position in php is very fast, especially in the case of large amount of data. However, it should be noted that "," should be added at the beginning and end, which is more rigorous. For example: user1,user2,user3, when looking for, check user1. There is also strpos to use! = = false, because the first one returns 0. Examples are as follows:

The copy code is as follows:

/ * *

* in_array is too slow when array is large

, /

Public static function inArray ($item, $array) {

$str = implode (',', $array)

$str =','. $str. ','

$item =','. $item. ','

Return false! = = strpos ($item, $str)? True: false

}

This is the end of "how to understand PHP in_array function". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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