PHP array de-weight relatively fast implementation, PHP array implementation _php Tutorial

Source: Internet
Author: User

PHP array de-weight faster implementation of the way, the implementation of the PHP array


Overview

Using PHP's Array_unique () function allows 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 the Array_unique () function in a large array, it will run slowly.

There is a better and faster function array_flip () instead of using the Array_unique () 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 the same result as the Array_unique () function.

Faster way to implement PHP array de-weight

/* Create an array of four elements with duplicate values */$array = array (' green ', ' blue ', ' orange ', ' blue ');/* Flip the array and you will get an array of unique key values (' green ' =>0, ' Blue ' =>1, ' orange ' =>2); */$array = Array_flip ($array);/* Then flip it again, reposition the keys and values, and get the 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 ...

 

The second way, use Array_keys

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

Conclusion

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

Articles you may be interested in:

    • Analysis of the de-weight problem of PHP two-dimensional array
    • PHP array de-weight function code
    • PHP three-dimensional array de-weight (sample code)
    • Example and analysis of PHP array de-weight
    • PHP array de-duplication example
    • Php bubble Sort, quick sort, quick find, two-dimensional array to re-share instances
    • PHP two-dimensional array merging and de-duplication method

http://www.bkjia.com/PHPjc/1093707.html www.bkjia.com true http://www.bkjia.com/PHPjc/1093707.html techarticle PHP array deduplication faster implementation, the PHP array implementation method overview using PHP's Array_unique () function allows you to pass an array, and then remove the duplicate values, return a unique ...

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