PHP uses asort to solve the problem of invalid Chinese sorting ,. PHP uses asort to solve the problem of invalid Chinese sorting. PHP has a very convenient method for re-sorting arrays-asort. For more information about how to use asort, see here. However, PHP uses asort to solve the problem of invalid Chinese sorting,
PHP has a very convenient method for re-sorting arrays-asort. For more information about how to use asort, see here. However, when asort sorts arrays containing Chinese keys, it sometimes does not follow the alphabetic order. This is mainly the encoding problem, if the encoding is UTF-8, it will not be arranged in alphabetical order, the solution is to first convert to GBK encoding, sorting and then back to the UTF-8.
Example: there is an array $ pass with a structure similar
Array ([0] => stdClass Object ([username] => John [password] => DQ9uqQW2 + udoszpqmnyvgg9l + example + LrSslRx9eSqU/nw.slsua =) [1] => stdClass Object ([username] => Li Si [password] => 2 P/3j50ibk1BYmjHL + 7/tests/Xdv/7Bu4pJAQ =) [2] => stdClass Object ([username] => Wang Wu [password] => caq8lq0l6uxJPRx + sCBsBFUojSF + ox98gwO6c/AquXQ/y/aj/l/ziegsxclr + signature =) [3] => stdClass Object ([username] => Zhao Liu [password] => Ghost/RyMxzJGQ = ))
The following code sorts the array by username in 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);}
Sort, PHP has a very convenient method to re-sort the array-asort, you can refer to the usage of asort here. But...