Sort the PHP array for questions, solve

Source: Internet
Author: User
PHP array sorting problem, solve!
/**
* The array is sorted by username from left to right, username the same as cardid from big to small
*/
$list = Array (
0=>array (' Cardid ' =>1001, ' username ' = ' zhangsan '),
1=>array (' Cardid ' =>1002, ' username ' = ' Zhangsanfeng '),
2=>array (' Cardid ' =>1003, ' username ' = ' sbzhangsan '),
3=>array (' Cardid ' =>1005, ' username ' = ' sbzhangsanisyou '),
4=>array (' Cardid ' =>1007, ' username ' = ' zhangsan '),
);
Match string: $q = ' Zhangsan ';

The sorted result should be
$list = Array (
0=>array (' Cardid ' =>1007, ' username ' = ' zhangsan '),
1=>array (' Cardid ' =>1001, ' username ' = ' zhangsan '),
2=>array (' Cardid ' =>1003, ' username ' = ' Zhangsanfeng '),
3=>array (' Cardid ' =>1005, ' username ' = ' sbzhangsanisyou '),
4=>array (' Cardid ' =>1007, ' username ' = ' sbzhangsan '),
);


------Solution--------------------
SQL to not
SQL Code
SELECT * FROM table WHERE username like '%zhangsan% ' ORDER by LOCATE (' Zhangsan ', username) ASC, Cardid DESC
------Solution--------------------
People want to sort the array, then sort the array.
PHP Code
$list = Array (0=>array (' Cardid ' =>1001, ' username ' = ' Zhangsan '), 1=>array (' Cardid ' =>1002, ' username ' = > ' Zhangsanfeng '), 2=>array (' Cardid ' =>1003, ' username ' = ' Sbzhangsan '), 3=>array (' Cardid ' =>1005, ' Username ' = ' sbzhangsanisyou '), 4=>array (' Cardid ' =>1007, ' username ' = ' Zhangsan '), $q = ' Zhangsan ';  foreach ($list as $v) {$r 1[] = $v [' Cardid ']; List ($r 2[], $r 3[]) = Explode ($q, $v [' username ']);} Array_multisort ($r 2, $r 3, $r 1, Sort_desc, $list);p Rint_r ($list);
------Solution--------------------
The landlord gave the result is contrary to his own description oh.

PHP Code
"Sort.php" 29L, 801C written [User:root time:02:58:29 path:/home/liangdong/php]$ php sort.php Array ([4] = =            Array ([Cardid] = 1007 [Username] = Zhangsan) [1] = = Array ( [Cardid] = 1002 [Username] = Zhangsanfeng) [0] = = Array ([car            did] = 1001 [Username] = Zhangsan) [3] = = Array ([Cardid] + 1005            [Username] = sbzhangsanisyou) [2] = = Array ([Cardid] = 1003 [Username] = Sbzhangsan)) [User:root time:02:58:31 path:/home/liangdong/php]$ Cat sort.php
      1001, ' username ' = ' Zhangsan '), Array (' Cardid ' =>1002, ' username ' = ' Zhangsanfeng '), Array (' Cardid ' =>1003, ' username ' = ' Sbzhangsan '), Array (' Cardid ' =>1005, ' Usernam E ' = ' sbzhangsanisyou '), Array (' Cardid ' =>1007, ' username ' = ' Zhangsan '), $na                                me = "Zhangsan"; Uasort ($list, function ($left, $right) {global $name;                                $lndx = Strpos ($left [' username '], $name);                                $rndx = Strpos ($right [' username '], $name);                                $lid = $left [' Cardid '];                                $rid = $right [' Cardid '];                                         if ($lndx = = = False && $rndx!== false | |  $lndx!== false && $rndx = = = False) {//not include the name of the small return $lndx = = = False?                        -1:1;        } if ($lndx = = $rndx) {return $rid-$lid;                                } else {return $lndx-$rndx; }});p Rint_r ($list);? >
  • 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.