3

这个问题是'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 字体,因此键盘中的每个 albhabet 代表 devnagari 中的某些字符。所以在devnagari中,c是第一位的,s是第二位的,以此类推。

任何帮助将不胜感激。提前致谢

4

1 回答 1

1

这是因为使用 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[');请让我知道它是否适用于您。

于 2012-08-09T09:49:22.127 回答