帮助在PHP中自定义排序

时间:2021-11-24 00:57:18

i'm trying to sort an array, such as

我正在尝试对数组进行排序,例如

$arr =('1000'=>'DUMMY',
       '100001'=>'DUMMY1',
       '100002'=>'DUMMY3',
       '100004'=>'DUMMY4',
       '1001'=>'TEST',
       '100100'=>'test1',
       '100102'=>'DUMMY123');

after sorting the result should be like this

排序后的结果应该是这样的

'1000'=>'DUMMY'
'100001'=>'DUMMY1'
'100002'=>'DUMMY3'
'100004'=>'DUMMY4'
'1001'=>'TEST'
'100100'=>'test1'
'100102'=>'DUMMY123'

both values and keys, should be strings.

值和键都应该是字符串。

Any help? Thanks a lot

有帮助吗?非常感谢

*******UPDATE******

******* UPDATE ******

Well, probably i was misunderstood. I will try to give more details.

好吧,可能是我被误解了。我会尝试提供更多细节。

User inputs two fields (both strings). The first one is a numeric one while the second is an alphanumeric one. So the aforementioned array gets constructed. The first field refers to an (unique) id while the second is the description of this id.

用户输入两个字段(两个字符串)。第一个是数字的,而第二个是字母数字的。所以上面提到的数组就构建了。第一个字段引用(唯一)id,而第二个字段引用此id。

IDs follow this pattern:

ID遵循以下模式:

There are some "basic" ids consisted of 4 digit. Let's name them 'categories'. These ids get analyzed to some other ids (sub-categories). Each sub-category has two digits which we add to the end of the id of the category. An example:

有一些“基本”id由4位数组成。我们将它们命名为“类别”。这些ID被分析到其他一些ID(子类别)。每个子类别都有两个数字,我们将其添加到类别ID的末尾。一个例子:

10.00 (cars)
  10.00.01 (blue cars)
    10.00.01.01 (blue cars, trucks)
    10.00.01.02 (blue cars, buses)
10.00.02 (red cars)
   10.00.02.01 (red cars, trucks)

.... etc

......等

users add/edit/delete categories/sub-categories. One should get the sorted array such as the example.

用户添加/编辑/删除类别/子类别。一个人应该得到排序的数组,例如示例。

Any help?

有帮助吗?

Thanks a lot

非常感谢

3 个解决方案

#1


1  

You did know PHP has excellent documentation? Take a look at asort. If you need to do a case-insensitive comparison, you may need uasort.

你知道PHP有很好的文档吗?看看asort。如果您需要进行不区分大小写的比较,则可能需要uasort。

#2


0  

I'm not really sure understand what you want.
But i guess it's something like that:

我不太清楚你想要什么。但我猜它是这样的:

this sort the array

这种数组

1st : by the first 4 digits of the key
2nd : by the last 2 digits if they're present


$arr = array(
    '100102'  => 'DUMMY123',
    '100100'  => 'test1',
    '1000'    => 'DUMMY',
    '100004'  => 'DUMMY4',
    '100001'  => 'DUMMY1',
    '100002'  => 'DUMMY3',
    '1001'    => 'TEST',
);

function mysort($a, $b) {
    preg_match('/^(\d{4})(\d\d)?$/', $a, $ma);
    preg_match('/^(\d{4})(\d\d)?$/', $b, $mb);

    if ($ma[1] == $mb[1]) {
        if (!isset($ma[2])) $ma[2] = '';
        if (!isset($mb[2])) $mb[2] = '';
        return strcmp($ma[2], $mb[2]);
    }
    return strcmp($ma[1], $mb[1]);
}
uksort($arr, 'mysort');
print_r($arr);

Output:

Array
(
    [1000] => DUMMY
    [100001] => DUMMY1
    [100002] => DUMMY3
    [100004] => DUMMY4
    [1001] => TEST
    [100100] => test1
    [100102] => DUMMY123
)

#3


0  

asort() should do the trick no matter how many extra 2-character subcategories you add. With the SORT_STRING flag the category doesn't even have to be a string.

无论您添加多少额外的2个字符的子类别,asort()都应该可以完成。使用SORT_STRING标志,类别甚至不必是字符串。

$arr =('100001'=>'DUMMY1',
       '1000'=>'DUMMY',
       '1001'=>'TEST',
       '100002'=>'DUMMY3',
       '100004'=>'DUMMY4',
       '100102'=>'DUMMY123',
       '100100'=>'test1');

asort($arr, SORT_STRING);

Should result in

应该导致

$arr =('1000'=>'DUMMY',
       '100001'=>'DUMMY1',
       '100002'=>'DUMMY3',
       '100004'=>'DUMMY4',
       '1001'=>'TEST',
       '100100'=>'test1',
       '100102'=>'DUMMY123');

#1


1  

You did know PHP has excellent documentation? Take a look at asort. If you need to do a case-insensitive comparison, you may need uasort.

你知道PHP有很好的文档吗?看看asort。如果您需要进行不区分大小写的比较,则可能需要uasort。

#2


0  

I'm not really sure understand what you want.
But i guess it's something like that:

我不太清楚你想要什么。但我猜它是这样的:

this sort the array

这种数组

1st : by the first 4 digits of the key
2nd : by the last 2 digits if they're present


$arr = array(
    '100102'  => 'DUMMY123',
    '100100'  => 'test1',
    '1000'    => 'DUMMY',
    '100004'  => 'DUMMY4',
    '100001'  => 'DUMMY1',
    '100002'  => 'DUMMY3',
    '1001'    => 'TEST',
);

function mysort($a, $b) {
    preg_match('/^(\d{4})(\d\d)?$/', $a, $ma);
    preg_match('/^(\d{4})(\d\d)?$/', $b, $mb);

    if ($ma[1] == $mb[1]) {
        if (!isset($ma[2])) $ma[2] = '';
        if (!isset($mb[2])) $mb[2] = '';
        return strcmp($ma[2], $mb[2]);
    }
    return strcmp($ma[1], $mb[1]);
}
uksort($arr, 'mysort');
print_r($arr);

Output:

Array
(
    [1000] => DUMMY
    [100001] => DUMMY1
    [100002] => DUMMY3
    [100004] => DUMMY4
    [1001] => TEST
    [100100] => test1
    [100102] => DUMMY123
)

#3


0  

asort() should do the trick no matter how many extra 2-character subcategories you add. With the SORT_STRING flag the category doesn't even have to be a string.

无论您添加多少额外的2个字符的子类别,asort()都应该可以完成。使用SORT_STRING标志,类别甚至不必是字符串。

$arr =('100001'=>'DUMMY1',
       '1000'=>'DUMMY',
       '1001'=>'TEST',
       '100002'=>'DUMMY3',
       '100004'=>'DUMMY4',
       '100102'=>'DUMMY123',
       '100100'=>'test1');

asort($arr, SORT_STRING);

Should result in

应该导致

$arr =('1000'=>'DUMMY',
       '100001'=>'DUMMY1',
       '100002'=>'DUMMY3',
       '100004'=>'DUMMY4',
       '1001'=>'TEST',
       '100100'=>'test1',
       '100102'=>'DUMMY123');