简体   繁体   English

PHP数组排序有两种方式

[英]php array sort in two ways

I have an array: 我有一个数组:

$arr = array(

    'Alcatel' => '0',
    'Apple' =>   '4542',
    'LG' =>      '0',
    'Nokia' =>   '0',
    'Samsung' => '2760',
    'Siemens' => '0',
    'Sony' =>    '0',

);

all the keys are alphabetically ordered, but the values are not. 所有键都是按字母顺序排列的,但是值却不是。 I want to: 1- maintain associations 2- sort the array by values HIGH to LOW 3- maintain alphabetical order for zero-value entries 我要:1-维护关联2-按从高到低的值对数组排序3-保持零值条目的字母顺序

to get: 要得到:

$arr = array(

    'Apple' =>   '4542',
    'Samsung' => '2760',
    'Alcatel' => '0',
    'LG' =>      '0',
    'Nokia' =>   '0',
    'Siemens' => '0',
    'Sony' =>    '0',

);

now when I used arsort($arr); 现在当我使用arsort($ arr);

I got: 我有:

$arr = array(
  'Apple' =>   '4542',
  'Samsung' => '2760',
  'Siemens' => '0',
  'Sony' =>    '0',
  'Nokia' =>   '0',
  'LG' =>      '0',
  'Alcatel' => '0',
);

So I got goals 1 and 2 and NOT 3 所以我得到了目标1和2,而不是3

Any suggestion? 有什么建议吗?

Thanks. 谢谢。

Ok, I finally did it: 好的,我终于做到了:

Because $arr is a combination of 2 arrays, $titles and $values: 因为$ arr是2个数组的组合,所以$ titles和$ values组成:

array_multisort($values, SORT_DESC, $titles, SORT_ASC, $arr);

this finally works! 终于可以了!

For the more general case, if you didn't have the keys and values already handy in separate source arrays, you would need a custom comparison function. 对于更一般的情况,如果在单独的源数组中没有方便的键和值,则需要一个自定义比较功能。 There are versions of sort that pass the keys to the comparison function, and versions that pass the values, but you need both, and there isn't one that passes both in. 有将密钥传递给比较函数的sort版本,以及传递值的版本,但是您需要两者,并且没有一个将两者都传递的。

Since uksort passes the keys, you can look up the values, but if you access the array being sorted from the comparison function, you get unreliable results and a warning from PHP. 由于uksort传递键,因此您可以查找值,但是如果从比较函数访问要排序的数组,则会得到不可靠的结果,并且PHP会发出警告。 So you need to make a copy. 因此,您需要进行复制。

The cleanest solution I could think of was to wrap the copy/access up in a closure (so this requires 5.4): 我能想到的最干净的解决方案是将副本/访问包装在一个封闭的容器中(因此这需要5.4):

$arr = array(
    'Alcatel' => '0',
    'Apple' =>   '4542',
    'LG' =>      '0',
    'Nokia' =>   '0',
    'Samsung' => '2760',
    'Siemens' => '0',
    'Sony' =>    '0',
  );

  function mkcmp($arr) {
    return function($k1, $k2) use ($arr) {
      $v1 = $arr[$k1];
      $v2 = $arr[$k2];

      if ($v1 > $v2) {
        return -1;
      } else if ($v1 < $v2) {
        return 1;
      } else if ($k1 < $k2) {
        return -1;
      } else if ($k1 == $k2) {
        return 0;
      } else {
        return 1;
      }
    };
  }

  uksort($arr, mkcmp($arr));

And here's the output of print_r($arr) after the sort: 这是排序后的print_r($arr)的输出:

Array
(
    [Apple] => 4542
    [Samsung] => 2760
    [Alcatel] => 0
    [LG] => 0
    [Nokia] => 0
    [Siemens] => 0
    [Sony] => 0
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM