简体   繁体   English

按十进制值对多维数组进行排序

[英]Sort Multi-dimensional array by decimal values

What I'm trying to do is sort a multi-dimensional array that contains decimal values.我想要做的是对包含十进制值的多维数组进行排序。 From what I've tested, floats are having trouble being ordered properly.根据我的测试,花车无法正确排序。

Array
(
    [0] => Array
        (
            [company] => Ebay
            [weight] => 4.6
        )

    [1] => Array
        (
            [company] => Ebay
            [weight] => 1.7
        )

    [2] => Array
        (
            [company] => Ebay
            [weight] => 3.7
        )
)


usort($array, 'order_by_weight');

// Sorts DESC highest first
function order_by_weight($a, $b) {
    return $b['weight'] - $a['weight'];
}

What is the best way to sort these numbers in descending?将这些数字降序排序的最佳方法是什么?

$arr = array(
    array('company' => 'A', 'weight' => 4.6),
    array('company' => 'B', 'weight' => 1.7),
    array('company' => 'C', 'weight' => 3.7),
);

usort($arr, 'order_by_weight');

function order_by_weight($a, $b) {
    return $b['weight'] > $a['weight'] ? 1 : -1;
}

var_dump($arr);

PS: it's not a rocket science - this exact "trick" is used as the first example at http://php.net/usort PS:这不是火箭科学 - 这个确切的“技巧”被用作http://php.net/usort的第一个例子

You can do this with anonymous function in just one line您可以在一行中使用匿名函数执行此操作

$arr = array(
    array('company' => 'A', 'weight' => 4.6),
    array('company' => 'B', 'weight' => 1.7),
    array('company' => 'C', 'weight' => 3.7),
);
usort($arr, function($a, $b) { return $b['weight'] > $a['weight'] ;});

print_r($arr);

Hope this helps :)希望这可以帮助 :)

You can sort the array using array_multisort, altough, this is often used to sort on multiple array values instead of one.您可以使用 array_multisort 对数组进行排序,尽管这通常用于对多个数组值而不是一个进行排序。

echo "<pre>";

 $a = array(
     array('company' => 'ebay', 'weight' => 4.6), 
     array('company' => 'ebay', 'weight' => 1.7),
     array('company' => 'ebay', 'weight' => 3.7),
     array('company' => 'ebay', 'weight' => 2.7),
     array('company' => 'ebay', 'weight' => 9.7),
     array('company' => 'ebay', 'weight' => 0.7),
 );

 $company = array();
 $weight = array();

 foreach($a as $key=>$val) {
     array_push($company, $val['company']);
     array_push($weight, $val['weight']);
 }

 array_multisort($weight, SORT_ASC, $a);

 print_r($a);

As sorting algo here is good example for sort multi dimension array without using any more inbuilt php function由于这里的排序算法是对多维数组进行排序而不使用任何更多内置 php 函数的好例子

$multiarr = array('0'=>array(
        "hashtag" => "a7e87329b5eab8578f4f1098a152d6f4",
        "title" => "Flower",
        "order" => 3),

'1' => array(
        'hashtag' => "b24ce0cd392a5b0b8dedc66c25213594",
        "title" => "Free",
        "order" => 2),
'2' => array('hashtag' => 'e7d31fc0602fb2ede144d18cdffd816b',
        'title' => 'Ready',
        'order' => 1
    ));

sorting function :排序功能:

    function multisort (&$array, $key) {
$valsort=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
    $valsort[$ii]=$va[$key];
}
asort($valsort);
foreach ($valsort as $ii => $va) {
    $ret[$ii]=$array[$ii];
}
$array=$ret;

} }

multisort($multiarr,"order")enter code here multisort($multiarr,"order") 在此处输入代码

: output : : 输出 :

Array
 (
[2] => Array
    (
        [hashtag] => e7d31fc0602fb2ede144d18cdffd816b
        [title] => Ready
        [order] => 1
    )

[1] => Array
    (
        [hashtag] => b24ce0cd392a5b0b8dedc66c25213594
        [title] => Free
        [order] => 2
    )

[0] => Array
    (
        [hashtag] => a7e87329b5eab8578f4f1098a152d6f4
        [title] => Flower
        [order] => 3
    )

) )

In case someone wants a more concise code, especially to handle equals condition you will add if condition before @zerkms's solution如果有人想要更简洁的代码,尤其是处理相等条件,您将在@zerkms 的解决方案之前添加 if 条件

using ceil will round fractions up and will sort the decimal numbers correctly使用ceil会将分数四舍五入并对小数进行正确排序

usort($data, function($a, $b) 
      {
        return ceil($a[$_GET['sortby']] - $b[$_GET['sortby']]); 
      });

None of the earlier posted answers are demonstrating the most modern syntaxes for sorting by a column value.较早发布的答案都没有演示按列值排序的最现代语法。

If using usort() , use the three-way comparison operator ("spaceship operator ") from PHP7 and if you are on PHP7.4 or higher enjoy the brevity of "arrow function" syntax.如果使用usort() ,请使用 PHP7 中的三向比较运算符(“宇宙飞船运算符”),如果您使用的是 PHP7.4 或更高版本,请享受“箭头函数”语法的简洁性。 For descending directional sorting write $b on the left of the operator and $a on the right.对于降序定向排序,在运算符的左侧写入$b ,在右侧写入 $ $a ( Demo ) 演示

usort($arr, fn($a, $b) => $b['weight'] <=> $a['weight']);

array_multisort() can also be used, but it requires an additional loop to isolate a column of data. array_multisort()也可以使用,但它需要一个额外的循环来隔离一列数据。 ( Demo ) 演示

array_multisort(array_column($arr, 'weight'), SORT_DESC, $arr);

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

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