Php顺序元素按值的一部分排列


Php order elements array by one part of value

例如,如果我有一个具有以下值的数组:

<?php
$aa=array(
"3456-898rterew",
"22-sdfsdf78",
"214-5548sdfsdf",
);
?>

我如何按数字对元素排序,直到"-",另一方面,我需要指定第二个数字,并显示从高到低的数字,然后将第二个号码放在"-"之后

我尝试这个:

<?php

for ($i=0;$i<count($aa);$i++)
{
$arr[]="".$aa[$i]."";
}

arsort($arr);

foreach($arr as $ar)
{
print "".$ar." <br>";
}

?>

在这种情况下,我的订单不好,我无法获得我想要的订单结果:

3456-898rterew
214-5548sdfsdf
22-sdfsdf78

直到订单是正确的,需要以这种方式显示

从更高的数字到更低的数字,如果没有,在"-"之后插入第二个数字,我得到了,但创建的数字是以这种方式创建的。

用于此任务PHPhttp://php.net/usort是正确的方法。如果任何标准数组排序方法都不适合您的任务,则可以定义一个自定义函数来对数组进行排序。

像这个

$aa=array(
    "3456-898rterew",
    "22-sdfsdf78",
    "214-5548sdfsdf"
);
usort($aa, function($a, $b){
    // separate string 
    $a = explode('-', $a, 2);
    $b = explode('-', $b, 2);
    // select first part before -
    $a = $a[0];
    $b = $b[0];
    // check if values are equal then do nothing
    if ($a == $b) {
        return 0;
    }
    // if $a bigger then $b - push array $a index lower then $b 
    return ($a > $b) ? -1 : 1;
});
print_R($aa);

输出

Array ( [0] => 3456-898rterew [1] => 214-5548sdfsdf [2] => 22-sdfsdf78 )

使用带有回调的usort()。使用array_map()explode()创建一个包含数字的数组。然后,使用array_combine()创建一个新数组,将原始数组的值作为关键字,将数字数组的值用作值:

代码:

usort($aa, function ($a, $b) use ($aa) {
    $arr = array_combine(
        array_values($aa), array_map(function($item) { 
            return explode('-', $item)[0]; 
        }, $aa)
    );
    return $arr[$a] < $arr[$b];
});
print_r($aa);

输出:

Array
(
    [0] => 3456-898rterew
    [1] => 214-5548sdfsdf
    [2] => 22-sdfsdf78
)

PHP允许您为排序函数usort()提供自己的比较函数,如果标准排序函数不合适,则可以使用该函数对数据进行排序。

首先,您的比较功能:

// printf calls are for illustration
function my_compare($a, $b) {
  printf("comparing %s to %s:  ", $a, $b);
  // split
  $a_parts = explode('-', $a);
  $b_parts  =explode('-', $b);
  //cast
  $a_int = intval($a_parts[0]);
  $b_int = intval($b_parts[0]);
  // compare
  if( $a_int < $b_int ) {
    printf("%s < %s 'n", $a, $b);
    return 1;
  } else if( $a_int == $b_int ) {
    printf("%s == %s 'n", $a, $b);
    return 0;
  } else {
    printf("%s > %s 'n", $a, $b);
    return -1;
  }
}

那么它就是简单的usort($aa, 'my_compare');

输出:

comparing 22-sdfsdf78 to 3456-898rterew:  22-sdfsdf78 < 3456-898rterew
comparing 214-5548sdfsdf to 22-sdfsdf78:  214-5548sdfsdf > 22-sdfsdf78
comparing 3456-898rterew to 214-5548sdfsdf:  3456-898rterew > 214-5548sdfsdf
array (
  0 => '3456-898rterew',
  1 => '214-5548sdfsdf',
  2 => '22-sdfsdf78',
)

对于PHP>=5.3:

usort($aa, function ($a, $b) {
    $a = array_shift(explode('-', $a));
    $b = array_shift(explode('-', $b));
    return ($a < $b);
});

PHP以面向对象的方式将SplHeap对象与本机标准PHP库(SPL)结合起来。因此,您可以构建一个简单的迭代器来解决您的问题。

class YourSplHeap extends SplHeap {
    public function compare( $value1, $value2 ) {
        $number1 = explode("-", $value1);
        $number2 = explode("-", $value2);
        return ($number1[0] - $number2[0]);
    }
}
$heap = new YourSplHeap();
$heap->insert("3456-898rterew");
$heap->insert("22-sdfsdf78");
$heap->insert("214-5548sdfsdf");
foreach ($heap as $entry) {
    echo $entry . "<br />";
}

显示的obove类是SPL的SplMaxHeap对象的变体。它比较所有插入,用"-"分隔它们,比较整数值并对它们进行排序,使最大值保持在顶部。因此,预期结果如下:

3456-898rterew
214-5548sdfsdf
22-sdfsdf78