Using Asort in PHP to deal with the problem of Chinese sort invalidation, _php tutorial

Source: Internet
Author: User

In PHP, the problem of Chinese sort failure is handled by using Asort,


PHP has a very convenient way to reorder the array of--asort, about the use of Asort can be seen here. However, when sorting an array containing Chinese keys, Asort sometimes does not follow the alphabetical order. This is mainly a coding problem, if encoded as UTF-8, it will not be sorted alphabetically, the solution is first converted to GBK encoding, sorted and then back to UTF-8.

Example: There is an array of $pass, similar in structure to

Array ([0] = StdClass Object  (   [Username] = Zhang San   [Password] = dq9uqqw2+uudoszpqmnyvgg9l+ rhzb6lrzbvdvael9uobnf2ztwunykhzfjhbzch+lrsslrx9esqu/n3gslsua==  ) [1] = = StdClass Object  (   [username ] = John Doe   [Password] = 2p/3j50ibk1bymjhl+7/tt0d6luoqmn9m8klxjczbcajqth5749jftth17wxibz9p425b4kiv/xdv/ 7bu4pjaq==  ) [2] = = StdClass Object  (   [username] = Harry   [Password] = caq8lq0l6uxjprx+ scbsbfuojsf+ox98gwo6c/aquxq/y/aj/l/ziegsxrsv+olck7ikojj4izzvx8dmpwzrra==  ) [3] = = StdClass Object  (   [Username] = Zhao Liu   [Password] = taxp4jx0vo3voflyanfgrsjzy76wqqhmnzyan9cyi20ukxlfmscxrfr3p525eimy0pg5zk8btbjos/rymxzjgq==  ) )

Using the following code, the array will be sorted according to the username pinyin.

foreach ($pass as $key) {$key->username = iconv (' UTF-8 ', ' GBK ', $key->username);} Asort ($pass), foreach ($pass as $key) {$key->username = iconv (' GBK ', ' UTF-8 ', $key->username);}




http://www.bkjia.com/PHPjc/865619.html www.bkjia.com true http://www.bkjia.com/PHPjc/865619.html techarticle PHP in the use of Asort for Chinese sorting failure problem processing, PHP has a very convenient way to reorder the array--asort, about the use of Asort can be seen here. But ... ..

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