使用usort在一定条件的基础上对多维数组进行排序

时间:2012-08-09 08:26:52

标签: php arrays 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);
});
?>

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

但是上面的代码没有按预期工作。它按顺序返回记录:

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

它应该按顺序返回

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

我使用自定义Devnagari字体,因此键盘中的每个albhabet代表devnagari中的某个字符。因此,在devnagari中,c首先出现,第二行等等。

任何帮助都将受到高度赞赏。非常感谢提前

1 个答案:

答案 0 :(得分:3)

这是因为使用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;
});

现在订单对字符没有限制。您可以指定static $orders = array('abc','a',';');static $orders = array('abcdef', 'b', 'z', ';', 'c[');之类的内容。如果有效,请告知我们。