Php 在多维字母数字数组上使用usort()(数字优先)
下面是我尝试排序的示例数组:Php 在多维字母数字数组上使用usort()(数字优先),php,arrays,sorting,usort,Php,Arrays,Sorting,Usort,下面是我尝试排序的示例数组: $array = (object)array( 'this' => 'that', 'posts'=> array( 'title' => '001 Chair', 'title' => 'AC43 Table', 'title' => '0440 Recliner', 'title' => 'B419', 'title' =>
$array = (object)array(
'this' => 'that',
'posts'=> array(
'title' => '001 Chair',
'title' => 'AC43 Table',
'title' => '0440 Recliner',
'title' => 'B419',
'title' => 'C10 Chair',
'title' => '320 Bed',
'title' => '0114'
),
'that' => 'this'
);
usort($array->posts, 'my_post_sort');
function my_post_sort($a, $b) {
$akey = $a->title;
if (preg_match('/^[0-9]*$',$akey,$matches)) {
$akey = sprintf('%010d ',$matches[0]) . $akey;
}
$bkey = $b->title;
if (preg_match('/^[0-9]*$',$bkey,$matches)) {
$bkey = sprintf('%010d ',$matches[0]) . $bkey;
}
if ($akey == $bkey) {
return 0;
}
return ($akey > $bkey) ? -1 : 1;
}
下面是我用来排序的函数:
$array = (object)array(
'this' => 'that',
'posts'=> array(
'title' => '001 Chair',
'title' => 'AC43 Table',
'title' => '0440 Recliner',
'title' => 'B419',
'title' => 'C10 Chair',
'title' => '320 Bed',
'title' => '0114'
),
'that' => 'this'
);
usort($array->posts, 'my_post_sort');
function my_post_sort($a, $b) {
$akey = $a->title;
if (preg_match('/^[0-9]*$',$akey,$matches)) {
$akey = sprintf('%010d ',$matches[0]) . $akey;
}
$bkey = $b->title;
if (preg_match('/^[0-9]*$',$bkey,$matches)) {
$bkey = sprintf('%010d ',$matches[0]) . $bkey;
}
if ($akey == $bkey) {
return 0;
}
return ($akey > $bkey) ? -1 : 1;
}
这给了我以下结果:
'posts', array(
'title' => 'C10 Chair',
'title' => 'B419',
'title' => 'AC43 Table',
'title' => '320 Bed',
'title' => '0440 Recliner',
'title' => '0114',
'title' => '001 Chair'
)
现在,我需要的最后一步是让数字显示在字母之前(降序)
以下是我的所需的输出:
'posts', array(
'title' => '320 Bed',
'title' => '0440 Recliner',
'title' => '0114',
'title' => '001 Chair',
'title' => 'C10 Chair',
'title' => 'B419',
'title' => 'AC43'
)
我尝试过各种排序、uasorts、preg_match和其他函数;而且似乎无法理解最后一步
有什么建议或帮助吗?谢谢。首先,我认为您的阵列无法工作。。。同一数组级别上不能多次使用同一密钥
foreach ($array as $key => $title) {
if ( is_numeric(substr($title, 0, 1)) ) {
$new_array[$key] = $title;
}
}
array_multisort($array, SORT_DESC, SORT_STRING);
array_multisort($new_array, SORT_DESC, SORT_NUMERIC);
$sorted_array = array_merge($array, $new_array);
尝试此比较功能:
function my_post_sort($a, $b) {
$akey = $a->title;
$bkey = $b->title;
$diga = preg_match("/^[0-9]/", $akey);
$digb = preg_match("/^[0-9]/", $bkey);
if($diga && !$digb) {
return -1;
}
if(!$diga && $digb) {
return 1;
}
return -strcmp($akey, $bkey);
}
它将按降序排序,但将数字放在其他符号之前。谢谢。它可以很好地工作,结果与我的函数相同;而且更干净。但是,最终的数组仍然没有先用数字排序。如何将数字(降序)移动到字母(降序)之前?谢谢。对不起,您想要desc,现在我修复它。数字总是小于字母,所以在您的情况下,这个函数就足够了。结果排序如下('C10椅子','B419',,'0440躺椅','0014'。我需要这样的('0440躺椅','0014…,'C10椅子','B419').是的,我搞砸了,字母更大…试试看。你必须为每个字母分配不同的键。不幸的是,不起作用。我认为标题的结构方式导致这不起作用??所有标题都可以/可能以字母结尾。请让我调整我的问题以反映额外的数组项。对不起,忘记添加“/”preg_中的符号匹配。更正。仍然不存在。我正在尝试设置一个php提琴。我仍然认为这与标题的结构有关。你是如何获得php lite提琴的url的?我有一个,但我不知道如何“抓取”它的url?感谢你持续的耐心!啊…明白了:)你是一个全明星!非常感谢你。现在我可以对你的代码进行反向工程,帮助我理解。非常感谢你。