对包含混合字符串php

时间:2016-09-14 12:29:08

标签: php arrays sorting

我在排序包含混合字符串的数组时遇到问题。

字符串包含数字和字符。我想用包含字符串的整数来对数组进行排序。

排序也应按字母顺序排列。

请查看Result数组和My Required数组。

My Current Array

Array
(
    [0] => Array
        (
            [title] => B2(14) Lorem ipsum dolor sit amet
        )

    [1] => Array
        (
            [title] => G3(14) consectetur adipiscing elit
        )

    [2] => Array
        (
            [title] => A3(14) Lorem ipsum dolor sit amet
        )

    [3] => Array
        (
            [title] => B13(16) consectetur adipiscing elit
        )

    [4] => Array
        (
            [title] => A13(16) consectetur adipiscing elit
        )

    [5] => Array
        (
            [title] => A13(15) consectetur adipiscing elit
        )

    [6] => Array
        (
            [title] => D13(15) Lorem ipsum dolor sit amet
        )

    [7] => Array
        (
            [title] => A13(13) consectetur adipiscing elit
        )

)

我用来排序数组的函数如下

uasort($array,'my_post_sort');

function my_post_sort($a,$b) {

   $akey = $a['title'];

   if (preg_match_all('/\d+/',$akey,$matches)) {

      $akey = sprintf('%010d ',$matches[0][1]);
   }
   $bkey = $b['title'];
   if (preg_match_all('/\d+/',$bkey,$matches)) {
      $bkey = sprintf('%010d ',$matches[0][1]);
   }

   if ($akey == $bkey) {

      return 0;
   }

   return ($bkey > $akey) ? 1 : -1;
}

我使用上述代码的结果如下

Array
(
    [3] => Array
        (
            [title] => B13(16) consectetur adipiscing elit
        )

    [4] => Array
        (
            [title] => A13(16) consectetur adipiscing elit
        )

    [5] => Array
        (
            [title] => A13(15) consectetur adipiscing elit
        )

    [6] => Array
        (
            [title] => D13(15) Lorem ipsum dolor sit amet
        )

    [0] => Array
        (
            [title] => B2(14) Lorem ipsum dolor sit amet
        )

    [1] => Array
        (
            [title] => G3(14) consectetur adipiscing elit
        )

    [2] => Array
        (
            [title] => A3(14) Lorem ipsum dolor sit amet
        )

    [7] => Array
        (
            [title] => A13(13) consectetur adipiscing elit
        )

)

我的要求如下

Array
(
    [0] => Array
        (
            [title] => A3(14) Lorem ipsum dolor sit amet
        )

    [1] => Array
        (
            [title] => B2(14) consectetur adipiscing elit
        )

    [2] => Array
        (
            [title] => G3(14) Lorem ipsum dolor sit amet
        )

    [3] => Array
        (
            [title] => A13(16) consectetur adipiscing elit
        )

    [4] => Array
        (
            [title] => B13(16) consectetur adipiscing elit
        )

    [5] => Array
        (
            [title] => A13(15) consectetur adipiscing elit
        )

    [6] => Array
        (
            [title] => D13(15) Lorem ipsum dolor sit amet
        )

    [7] => Array
        (
            [title] => A13(13) consectetur adipiscing elit
        )

)

Please see screenshot

1 个答案:

答案 0 :(得分:0)

根据我对你所要求的最好的解释 - 按字母后面的数字(不是parens中的那个)排序,然后是字母,你想要的结果'会有前两个条目错误的方式。

但试试这个,看看它是否适合你的要求(我也按照parens中的数字排序作为最后的手段):

$a = [
    ['title' => 'B2(14) Lorem ipsum dolor sit amet'],
    ['title' => 'G3(14) consectetur adipiscing elit'],
    ['title' => 'A3(14) Lorem ipsum dolor sit amet'],
    ['title' => 'B13(16) consectetur adipiscing elit'],
    ['title' => 'A13(16) consectetur adipiscing elit'],
    ['title' => 'A13(15) consectetur adipiscing elit'],
    ['title' => 'D13(15) Lorem ipsum dolor sit amet'],
    ['title' => 'A13(13) consectetur adipiscing elit'],
];

uasort($a, function($a, $b) {
    $partsA = $partsB = [];
    preg_match('!^([A-Z]+)(\d+)\((\d+)\) !i', $a['title'], $partsA);
    preg_match('!^([A-Z]+)(\d+)\((\d+)\) !i', $b['title'], $partsB);
    foreach ([2, 1, 3] as $i) {
        if ($partsA[$i] != $partsB[$i]) {
            return strnatcasecmp($partsA[$i], $partsB[$i]);
        }
    }
    return 0;
});

结果:

Array
(
    [0] => Array
        (
            [title] => B2(14) Lorem ipsum dolor sit amet
        )

    [2] => Array
        (
            [title] => A3(14) Lorem ipsum dolor sit amet
        )

    [1] => Array
        (
            [title] => G3(14) consectetur adipiscing elit
        )

    [7] => Array
        (
            [title] => A13(13) consectetur adipiscing elit
        )

    [5] => Array
        (
            [title] => A13(15) consectetur adipiscing elit
        )

    [4] => Array
        (
            [title] => A13(16) consectetur adipiscing elit
        )

    [3] => Array
        (
            [title] => B13(16) consectetur adipiscing elit
        )

    [6] => Array
        (
            [title] => D13(15) Lorem ipsum dolor sit amet
        )

)

虽然我可能误解了你重新阅读原帖的要求。你说'字母顺序'我在字符串的开头假设了这封信。当然,你可能意味着在parens之后的文本。所以这可能不是最适合您的需求,但可能有所帮助。