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

wfypjpf4  于 2023-02-03  发布在  PHP
关注(0)|答案(5)|浏览(135)

我正在尝试对一个包含小数值的多维数组进行排序。根据我的测试,浮点数在正确排序时遇到了问题。

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'];
}

按降序排列这些数字的最佳方法是什么?

elcex8rz

elcex8rz1#

$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:这不是什么高深的科学--这个“技巧”在http://php.net/usort中被用作第一个例子

r1wp621o

r1wp621o2#

您可以在一行中使用anonymous函数完成此操作

$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);

希望这有帮助:)

bprjcwpo

bprjcwpo3#

你可以使用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);
czfnxgou

czfnxgou4#

如果有人想要一个更简洁的代码,特别是处理等于条件时,可以在@zerkms的解之前添加if条件
使用ceil将向上舍入分数并正确排序十进制数

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

s71maibg5#

作为排序算法,这里是一个很好的例子,排序多维数组,而不使用任何更多的内置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
    ));

分拣功能:

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");

:输出:

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
    )

)

相关问题