How to sort php arrays in pinyin order-php Tutorial

Source: Internet
Author: User
The {code...} name field is converted to pinyin and then sorted by letters. if the first letter is the same and the second one is compared, the result is {code ...}.
php$ Pinyin = array ('name' => 'Liang Chaowei ', 'age' => 23), array ('name' => 'Andy ', 'age' => 24), array ('name' => 'Daybreak ', 'age' => 19), array ('name' => 'kwok City ', 'age' => 20 ));

The name field is converted to pinyin and then sorted by letters. if the first letter is the same as the second one, you want the result to be

php$ Pinyin = array ('name' => 'Andy ', 'age' => 24), array ('name' => 'kwok City ', 'age' => 20) array ('name' => 'Liang Chaowei ', 'age' => 23), array ('name' => 'day ', 'age' => 19 ),);

Find the Chinese character to pinyin database

Reply content:
php$ Pinyin = array ('name' => 'Liang Chaowei ', 'age' => 23), array ('name' => 'Andy ', 'age' => 24), array ('name' => 'Daybreak ', 'age' => 19), array ('name' => 'kwok City ', 'age' => 20 ));

The name field is converted to pinyin and then sorted by letters. if the first letter is the same as the second one, you want the result to be

php$ Pinyin = array ('name' => 'Andy ', 'age' => 24), array ('name' => 'kwok City ', 'age' => 20) array ('name' => 'Liang Chaowei ', 'age' => 23), array ('name' => 'day ', 'age' => 19 ),);

Find the Chinese character to pinyin database

Recently also encountered the problem of sorting Chinese characters, Collator effect is not bad, refer to Comparing UTF-8 String

$name = [...];(new \Collator('zh-CN'))->asort($name);var_dump( $name );

Before and after:

0 => 'Yuan Ziyang' 1 => 'duan jiming' 2 => 'Zhu gongwei' 3 => 'Zhu Wenyuan '4 => 'Zhu Fe' 5 => 'Lu zi gan '6 => 'Huang Fu yizhen' 7 => 'Zhong Zhong' 8 => 'Prince Shi '9 => 'Shi Sun junirong '10 => 'Yi cieming' 11 => 'Huang Shiying Sun '12 => 'Zhao Zirou '13 => 'Cai Bo qing' 14 => 'Cai Wenji' 15 => 'Yang wenxian' 16 => 'Yang de zu'17 => 'Wu Kuang '18 => 'mahoushu '19 => 'Zheng Gong Ye' 20 => 'Ding Jianyang' 21 => 'Wei Han ye'22 => 'hu Cai '23 => 'lile' 24 => 'Yang Feng' 25 => 'Zhang Yashu '26 => 'Yang ug' 27 => 'White Rabbit' 28 => 'Jiang shi' 29 => 'giben' 30 => 'givenran '31 => 'gisto' 32 => 'seasonal line' 33 => 'Division wei Huang zhi'34 => 'Lang Zhong Zhangjun'

=>

13 => 'Cai baizao' 14 => 'Cai Wenji '20 => 'Ding jianyang' 1 => 'duan jiming' 32 => 'seasonal line' 22 => 'hu cai '6 => 'Huang Fu yizhen' 11 => 'Huang Shiying Sun '29 => 'giben' 31 => 'gistro' 30 => 'givenran '28 => 'Jiang shi' 34 => 'Lang Zhong Zhangjun '23 => 'lile' 5 => 'Lu Zigan' 18 => 'mahoushu '9 => 'Shi Sun Junrong '33 => 'Wei zhihuang '27 => 'White Rabbit '8 => 'Prince Shi '21 => 'Wei Han qing' 17 => 'Wu Kuang' 10 => 'Yi cieming '7 => 'Yi Zhong' 26 => 'Yang ug' 16 => 'Yang DEZ' 24 => 'Yang Feng' 15 => 'Yang Wenxian '0 => 'Yuan Ziyang' 25 => 'Zhang Yaxiao '12 => 'Zhao Zirou' 19 => 'Zheng Gong Ye' 4 => 'Zhu Fe' 2 => 'Zhu gongwei' 3 => 'Zhu Wenyuan'

$ Sort = array (); foreach ($ pinyin as $ key = >$ item) {$ py = zhcnToPinyin ($ item ['name']); $ pinyin [$ key] ['Pinin'] = $ py; $ sort [] = $ py;} array_multisort ($ sort, SORT_ASC, $ pinyin); function zhcnToPinyin () {// Convert TODO Chinese characters to pinyin}

Upstairs, let's take a look at array_multisort ()

The Chinese character encoding table is in alphabetical order. Echo "Liang Chaowei" <"Dawn" returns true.
The sorting of pinyin is too lacking in basic knowledge.

@ IncNick in fact, the GBK encoding is not fully sorted by the first letter. you can check the GBK encoding table. I hope you will not be misled.

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.