这个问题是"这个问题"的后续问题。代码点火器代码返回数据库记录集,如以下数组所示。
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排在第二位,依此类推。
任何帮助将不胜感激。提前感谢一吨
这是因为使用 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[');
请让我知道它是否对您有效。