如何使用PHP对数组进行排序,忽略(先生,夫人)或一些文章

KAR*_*SRV 4 php arrays sorting

使用名称对数组进行排序我有一个数组.

 array(0 => Mr. Bala ,1 => Mr. Santhosh, 2 => Mrs. Camel,3 => Mrs. Vinoth); 
Run Code Online (Sandbox Code Playgroud)

仅根据名称按升序对其进行排序

我的预期产量是

array(
  0 => Mr. Bala,
  1 => Mrs. Camel,
  2 => Mr. Santhosh,
  3 => Mr. Vinoth,
);
Run Code Online (Sandbox Code Playgroud)

spl*_*h58 6

使用usort,取第二部分字符串,然后用空格分割

usort($a, function($i1, $i2) {
        return strcmp(explode('. ',$i1)[1], explode('. ',$i2)[1]);
      });
Run Code Online (Sandbox Code Playgroud)

UPD由于Bart Friederichs的评论

usort($a, function($i1, $i2) {
            $t = explode('. ',$i1);
            $i1 = (! isset($t[1]) ? $i1 : $t[1]);
            $t = explode('. ',$i2);
            $i2 = (! isset($t[1]) ? $i2 : $t[1]);
            return strcmp($i1, $i2);
          });
Run Code Online (Sandbox Code Playgroud)

演示

UPD2使其不区分大小写

usort($a, function($i1, $i2) {
            $t = explode('. ',$i1);
            $i1 = (! isset($t[1]) ? $i1 : $t[1]);
            $t = explode('. ',$i2);
            $i2 = (! isset($t[1]) ? $i2 : $t[1]);
            return strcmp(strtoupper($i1), strtoupper($i2));
          });
Run Code Online (Sandbox Code Playgroud)