2 Custom PHP in

Source: Internet
Author: User

  But if the array is larger, the performance will drop and it will run for a long time, so if you are optimizing for a large array, here are two methods (implemented by custom functions):

  1. Array key and value flip through isset to determine whether the key exists in the array     code is as follows:/**  * In_array is too slow the When array is large &nbsp ; */public static function InArray ($item, $array) {    $flipArray = Array_flip ($array);     Return is Set ($flipArray [$item]); You may also ask why you do not use array_key_exists to make judgments. isset? Here's a comparison between array_key_exists () and Isset (): Isset () does not return true for values that are null in the array, array_key_exists ().   Code as follows: <?php $search _array = array (' => null, ' second ' => 4);  //returns false Isset ($search _array[' a ']);  //Returns True Array_key_exists (' i ', $search _array);?>   2. Use implode connection, directly use Strpos to judge   use implode function + comma connected, directly with strpos judgment. PHP inside the string to take position very fast, especially in the case of large amount of data. However, it is necessary to pay attention to the end of the ",", so more rigorous. such as:, User1,user2,user3, find the time, check, User1,. There are also strpos to use!== false because the first one will return 0. Examples are as follows: The     code is as follows:/**  * In_array is too slow the When the array is large  */public static function InArray ($item , $array) {    $STR = implode (', ', $array);     $STR = ',' . $str. ',';     $item = ', '. $item. ',';     return false!== Strpos ($item, $str)? True:false; }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.