Faster way to implement PHP array de-weight

Source: Internet
Author: User
Original: Faster alternative to PHP ' s Array Unique Function

Overview

Functions that use PHP array_unique() allow you to pass an array and then remove the duplicate values, returning an array with unique values. This function works very well in most cases. However, if you try to use a function in a large array array_unique() , it will run slowly.

There is a better and faster function array_flip() to replace the use array_unique() of a function to create a unique array. This magical function swaps the keys and values of each element in the array, because the key values must be unique, so you get array_unique() the same result as the function.

Faster way to implement PHP array de-weight

/* 创建一个包含重复值的,一共四个元素的数组 */$array = array('green','blue','orange','blue');/* 翻转数组,你将会得到唯一键值的数组        array('green'=>0,'blue'=>1,'orange'=>2); */$array = array_flip($array);/* 然后再翻转一次,将键和值重新放置,然后得到数组:array(0=>'green',1=>'blue',2=>'orange'); */$array = array_flip($array);

Because we have removed some elements, the array does not appear to be a normal sequence. For example we may get: array(0=>'A',2=>'B',5=>'C'); . In some cases, this is not a problem, but if you need the array's key values to keep the sequence of numbers, you can use one or two methods to solve the problem of key-value disorder.

Keys for repairing arrays using Array_merge

After adding Array_flip functions, the key values of the array will be sorted and restored to the normal sequence, such as: 0,1,2,3 ...

$array = array('green','blue','orange','blue');$array = array_flip($array);$array = array_flip($array);/* 使用array_merge()函数修复键值*/$array = array_merge($array);

The second way, use Array_keys

Note that this method of repairing array key values is a little faster than using array_merge() functions. You can also use the function in conjunction with the last step array_keys() (This function returns the value after the rollover). Then when you flip the values of the array, the key values are created in order.

$array = array('green','blue','orange','blue');$array = array_flip($array);/* 跟第一个例子一样,但是现在我们先提取数组的键值 */$array = array_keys($array);

Conclusion

Very simple, there is an effective performance boost compared to using the Array_unique function in a large array.

The above describes a faster way to implement the PHP array to weight, including aspects of the content, I hope the PHP tutorial interested in a friend to help.

  • Related Article

    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.