编程语言
首页 > 编程语言> > php-使用usort根据特定条件对多维数组进行排序

php-使用usort根据特定条件对多维数组进行排序

作者:互联网

这个问题是从‘This question’开始的后续问题.codeigniter代码将数据库记录集作为以下数组返回.

Array
(
    [0] => stdClass Object
    (
        [user_id] => NLK32439
        [first_name] => sdn
        [last_name] => hf]zL
        [email_address] => user1@hotmail.com
        [mobile_number] => 9841349349
        [description] => g]kfn
        [date_joined] => 09-AUG-12
        [status] => 1
        [username] => user1
        [userpassword] => 691f9298642af07c2d6ea8fef56074201e077b34
    )

    [1] => stdClass Object
    (
        [user_id] => NLK94358
        [first_name] => alag
        [last_name] => k|wfg
        [email_address] => user2@gmail.com
        [mobile_number] => 823472384723
        [description] => g]kfn
        [date_joined] => 09-AUG-12
        [status] => 1
        [username] => user2
        [userpassword] => 691f9298642af07c2d6ea8fef56074201e077b34
    )

    [2] => stdClass Object
    (
        [user_id] => NLK32437
        [first_name] => ;lag
        [last_name] => %]qL
        [email_address] => user3@msn.com
        [mobile_number] => 9851112412
        [description] => g]kfn
        [date_joined] => 08-AUG-12
        [status] => 1
        [username] => user3
        [userpassword] => 691f9298642af07c2d6ea8fef56074201e077b34
    )

    [3] => stdClass Object
    (
        [user_id] => NLK32435
        [first_name] => clgn
        [last_name] => zdf{
        [email_address] => user4@msn.com
        [mobile_number] => 984134354
        [description] => g]kfn
        [date_joined] => 08-AUG-12
        [status] => 1
        [username] => user4
        [userpassword] => 0e025eade868b4b481f41ff7449bc1967261e170
    )

)

我要做的就是在一定条件下通过“ first_name”对数组进行排序.到目前为止,我的PHP代码是-

<?php
usort($array, function ($a, $b) {
    static $order = array('c', 's','a',';', 'L');
    return array_search($a->first_name, $order) - array_search($b->first_name, $order);
});
?>

我的意图是对数组进行排序,以便以“ c”开头的first_name排在第一位,以“ s”开头的第二个,以“ a”排在第三位,以“;”开头;排第四.

但是上面的代码无法正常工作.它按顺序返回记录:

's','a',';','c'

它应该按顺序返回

'c', 's','a',';'

我使用的是自定义的Devnagari字体,因此键盘上的每个字母都代表devnagari中的某些字符.因此在devnagari中,c排在第一,s在第二,依此类推.

任何帮助将不胜感激.提前感谢一吨

解决方法:

这是因为只有在字符串与数组中的某些字符串完全匹配时,使用array_search才会返回.

$array = array('a','b','c');
var_dump(array_search('blue',$array))
// This will output
bool(false)

因此,如果您愿意,可以尝试以下方法:

<?php
usort($array, function ($a, $b) {
static $order = array('c', 's','a',';', 'L');
return array_search(substr($a->first_name,0,1), $order) - array_search(substr($b->first_name,0,1), $order);
});
?>

希望这可以帮助.

更新:关于您的评论问题,我有个主意.请看一下.

usort($array, function($a, $b) {
    //example for the orders
    static $orders = array (';l', 'c', 's', 'a', ';', 'L');

    //variables to count the point for $a and $b (it is equal at first)
    $pointA = 1;
    $pointB = 1;

    //also variables to mark if $a or $b are matched on one of the orders
    $isFoundA = false;
    $isFoundB = false;

    //iterate foreach orders
    $i = 0; //this is something to add to the point, the more earlier the first_name found in the order, the less point will be added
    foreach ($orders as $order) {
        //if $a->first_name is still not found in orders and it has been just founded 
        if (!$isFoundA && strpos($a->first_name,$order) === 0) {
            $pointA += $i;
            $isFoundA = true; //we don't need to check this again since we had found it
        }
        //if $b->first_name is still not found in orders and it has been just founded 
        if (!isFoundB && strpos($b->first_name,$order) === 0) {
            $pointB += $i;
            $isFoundB = true; //the same as above, we don't need to check it again
        }
        $i++;
    }
    //after iterate in orders, we check the points for $a and $b
    if ($pointA == $pointB) return 0;
    else return ($pointA < $pointB) ? -1 : 1;
});

现在,顺序对字符没有限制.您可以指定类似静态的$orders = array(‘abc’,’a’,’;’);或静态$orders = array(‘abcdef’,’b’,’z’,’;’,’c [‘);请让我知道它是否对您有效.

标签:usort,arrays,php
来源: https://codeday.me/bug/20191127/2077048.html