PHP 使用特定字母对数组进行排序

发布于 2024-11-28 07:53:59 字数 1017 浏览 2 评论 0原文

我有一个如下所示的 php 数组:

Array ( 
[0] => Array ( [value] => 5 [label] => Akon )
[1] => Array ( [value] => 6 [label] => Angel ) 
[2] => Array ( [value] => 7 [label] => Britny ) 
[3] => Array ( [value] => 9 [label] => Mark Anthony ) 
[4] => Array ( [value] => 8 [label] => Michel ) 
[5] => Array ( [value] => 4 [label] => Shaggy ) 
[6] => Array ( [value] => 3 [label] => Smith ) 
) 

我需要这个数组按特定字母排序。例如,如果我按“M”字母数组排序,则应如下所示。

Array ( 
[3] => Array ( [value] => 9 [label] => Mark Anthony ) 
[4] => Array ( [value] => 8 [label] => Michel ) 
[6] => Array ( [value] => 3 [label] => Smith ) 
[0] => Array ( [value] => 5 [label] => Akon ) 
[1] => Array ( [value] => 6 [label] => Angel ) 
[2] => Array ( [value] => 7 [label] => Britny ) 
[5] => Array ( [value] => 4 [label] => Shaggy ) 
) 

乞求信应该出现在数组的第一个。(这里以m开头) 我非常感谢您提供的任何帮助。非常感谢...

I have an php array like below:

Array ( 
[0] => Array ( [value] => 5 [label] => Akon )
[1] => Array ( [value] => 6 [label] => Angel ) 
[2] => Array ( [value] => 7 [label] => Britny ) 
[3] => Array ( [value] => 9 [label] => Mark Anthony ) 
[4] => Array ( [value] => 8 [label] => Michel ) 
[5] => Array ( [value] => 4 [label] => Shaggy ) 
[6] => Array ( [value] => 3 [label] => Smith ) 
) 

I need this array sort by specific letter. For example, if I sort by this "M" letter array should look like below.

Array ( 
[3] => Array ( [value] => 9 [label] => Mark Anthony ) 
[4] => Array ( [value] => 8 [label] => Michel ) 
[6] => Array ( [value] => 3 [label] => Smith ) 
[0] => Array ( [value] => 5 [label] => Akon ) 
[1] => Array ( [value] => 6 [label] => Angel ) 
[2] => Array ( [value] => 7 [label] => Britny ) 
[5] => Array ( [value] => 4 [label] => Shaggy ) 
) 

The begging letter should comes to first of array.(here begin with m)
I greatly appreciated your any kind of help. Thank you very much...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

活雷疯 2024-12-05 07:53:59

你的比较逻辑将是这样的:

  • 如果两个字符串 A 和 B 以相同的字母开头,则照常比较它们,
  • 如果 A 以 'M' 开头,
  • 如果 B 以 'M' 开头,则 A 获胜,
  • 否则 B 获胜,比较为通常

在代码中

$strings = array('Foo', 'Moo', 'Xuux', 'Me', 'Blah', 'Ma');
$letter = 'M';

usort($strings, function($a, $b) use($letter) {
    if($a[0] != $b[0]) {
        if($a[0] == $letter) return -1;
        if($b[0] == $letter) return +1;
    }
    return strcmp($a, $b);  
});

print_r($strings);

Your comparison logic is going to be like this:

  • if two strings A and B start with the same letter, compare them as usual
  • if A starts with 'M', A wins
  • if B starts with 'M', B wins
  • otherwise, compare as usual

in code

$strings = array('Foo', 'Moo', 'Xuux', 'Me', 'Blah', 'Ma');
$letter = 'M';

usort($strings, function($a, $b) use($letter) {
    if($a[0] != $b[0]) {
        if($a[0] == $letter) return -1;
        if($b[0] == $letter) return +1;
    }
    return strcmp($a, $b);  
});

print_r($strings);
谁把谁当真 2024-12-05 07:53:59
class Cmp {
    public $letter;

    function __construct( $letter ) { $this->letter = $letter; }

    function doCmp( $a, $b ) {
        if( $a['label'][0] == $this->letter ) {
            if( $b['label'][0] != $this->letter ) return -1;
        } else {
            if( $b['label'][0] == $this->letter ) return 1;
        }
        return $a['label'] > $b['label'] ? 1 : -1;
    }
}

usort( $arr, array( new Cmp( 'M' ),  'doCmp' ) );
class Cmp {
    public $letter;

    function __construct( $letter ) { $this->letter = $letter; }

    function doCmp( $a, $b ) {
        if( $a['label'][0] == $this->letter ) {
            if( $b['label'][0] != $this->letter ) return -1;
        } else {
            if( $b['label'][0] == $this->letter ) return 1;
        }
        return $a['label'] > $b['label'] ? 1 : -1;
    }
}

usort( $arr, array( new Cmp( 'M' ),  'doCmp' ) );
晨敛清荷 2024-12-05 07:53:59
$strings = array ( 
    array ( 'value' => 5, 'label' => 'Akon' ),
    array ( 'value' => 6, 'label' => 'Angel' ), 
    array ( 'value' => 7, 'label' => 'Britny' ), 
    array ( 'value' => 9, 'label' => 'Mark Anthony' ) ,
    array ( 'value' => 8, 'label' => 'Michel' ) ,
    array ( 'value' => 4, 'label' => 'Shaggy' ) ,
    array ( 'value' => 3, 'label' => 'Smith' ) 
) ;

var_dump($strings);


$letter = 'm';
usort ($strings, function ($left, $right) {
    return ((($posLeft = strpos(strtolower($left['label']), 'm')) === false)
                       ? PHP_INT_MAX
                       : $posLeft)
         - ((($posRight = strpos(strtolower($right['label']), 'm')) === false) 
                       ? PHP_INT_MAX 
                       : $posRight);
});
var_dump($strings);

只是比较两个字符串中字母的位置。如果该字母不在字符串之一内(strpos() 返回 false),则假定为“无限”索引 (PHP_INT_MAX),

$strings = array ( 
    array ( 'value' => 5, 'label' => 'Akon' ),
    array ( 'value' => 6, 'label' => 'Angel' ), 
    array ( 'value' => 7, 'label' => 'Britny' ), 
    array ( 'value' => 9, 'label' => 'Mark Anthony' ) ,
    array ( 'value' => 8, 'label' => 'Michel' ) ,
    array ( 'value' => 4, 'label' => 'Shaggy' ) ,
    array ( 'value' => 3, 'label' => 'Smith' ) 
) ;

var_dump($strings);


$letter = 'm';
usort ($strings, function ($left, $right) {
    return ((($posLeft = strpos(strtolower($left['label']), 'm')) === false)
                       ? PHP_INT_MAX
                       : $posLeft)
         - ((($posRight = strpos(strtolower($right['label']), 'm')) === false) 
                       ? PHP_INT_MAX 
                       : $posRight);
});
var_dump($strings);

Just compares the position of the letter within the two strings. If the letter is not within one of the strings (strpos() returns false), it assumes an "infinite" index (PHP_INT_MAX),

无远思近则忧 2024-12-05 07:53:59

这是我在 php.net 上找到的一个示例,可以帮助您维护索引: http://www.php.net/manual/en/function.sort.php#99419

this is an example i found on php.net that could help you and maintain the index : http://www.php.net/manual/en/function.sort.php#99419

忆依然 2024-12-05 07:53:59

基于反映字母比较,按数据进行排序(演示):

<?php

# the data
$data = array(
  0 => array(
    'value' => 5,
    'label' => 'Akon',
  ),
  1 => array(
    'value' => 6,
    'label' => 'Angel',
  ),
  2 => array(
    'value' => 7,
    'label' => 'Britny',
  ),
  3 => array(
    'value' => 9,
    'label' => 'Mark Anthony',
  ),
  4 => array(
    'value' => 8,
    'label' => 'Michel',
  ),
  5 => array(
    'value' => 4,
    'label' => 'Shaggy',
  ),
  6 => array(
    'value' => 3,
    'label' => 'Smith',
  ),
);

# the letter
$letter = 'M';

# the value to compare against
$value = function(array $a)
{
  $key = 'label';
  if (!array_key_exists($key, $a))
  {
     throw new InvalidArgumentException(sprintf('Key "%s" missing.', $key));
  }
  return $a[$key];
};

# the comparison
$compare = function($a, $b) use ($letter, $value) 
{
  $a = $value($a);
  $b = $value($b);
  if($a[0] != $b[0]) {
    if($a[0] === $letter) return -1;
    if($b[0] === $letter) return +1;
  }
  return strcmp($a, $b);  
};

# the sort
$sort = function() use ($data, $compare)
{
  $r = uasort($data, $compare);
  if (!$r)
  {
    throw new RuntimeException('Sort failed.');
  }
  return $data;
};

print_r($sort());

Sort the data by the value based on the comparison reflecting the letter (Demo):

<?php

# the data
$data = array(
  0 => array(
    'value' => 5,
    'label' => 'Akon',
  ),
  1 => array(
    'value' => 6,
    'label' => 'Angel',
  ),
  2 => array(
    'value' => 7,
    'label' => 'Britny',
  ),
  3 => array(
    'value' => 9,
    'label' => 'Mark Anthony',
  ),
  4 => array(
    'value' => 8,
    'label' => 'Michel',
  ),
  5 => array(
    'value' => 4,
    'label' => 'Shaggy',
  ),
  6 => array(
    'value' => 3,
    'label' => 'Smith',
  ),
);

# the letter
$letter = 'M';

# the value to compare against
$value = function(array $a)
{
  $key = 'label';
  if (!array_key_exists($key, $a))
  {
     throw new InvalidArgumentException(sprintf('Key "%s" missing.', $key));
  }
  return $a[$key];
};

# the comparison
$compare = function($a, $b) use ($letter, $value) 
{
  $a = $value($a);
  $b = $value($b);
  if($a[0] != $b[0]) {
    if($a[0] === $letter) return -1;
    if($b[0] === $letter) return +1;
  }
  return strcmp($a, $b);  
};

# the sort
$sort = function() use ($data, $compare)
{
  $r = uasort($data, $compare);
  if (!$r)
  {
    throw new RuntimeException('Sort failed.');
  }
  return $data;
};

print_r($sort());
〃安静 2024-12-05 07:53:59

使用 usort 按函数排序。该函数可以包含您想要放入的任何逻辑,只要返回值指示排序顺序即可(有关详细信息,请参阅链接)。

Use usort to sort by a function. The function can contain any logic you want to put in, as long as the return value indicates sort order (see link for details).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文