PHP - 根据数组键排序数组

5

我在PHP中有这个数组:

在PHP API中,我有这个数组并想按照custom_price进行排序,但不知道如何实现...

Array
(
    [0] => Array
        (
            [id] => 1204
            [custom_price] => 33.1500
        )


    [1] => Array
        (
            [id] => 1199
            [custom_price] => 16.83
        )

    [2] => Array
        (
            [id] => 1176
            [custom_price] => 16.83
        )

    [3] => Array
        (
            [id] => 1173
            [custom_price] => 11.73
        )

    [4] => Array
        (
            [id] => 1170
            [custom_price] => 22.5
        )
)

我该如何按照自定义价格从高到低或从低到高进行排序?
6个回答

9

使用 usort

从高到低排序

usort($input, function ($a, $b) {return $a['custom_price'] < $b['custom_price'];});
print_r( $input );

从低到高
usort($input, function ($a, $b) {return $a['custom_price'] > $b['custom_price'];});
print_r( $input );

http://php.net/manual/en/function.usort.php


1
这个解决方案可能会对您有帮助。
function sortByOrder($a, $b) {
    return $a['custom_price'] - $b['custom_price'];
}

usort($myArray, 'sortByOrder');

或者

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

aasort($your_array,"custom_price");

这里是参考 链接

1
使用array_multisort()函数,其中包括SORT_DESCSORT_ASC参数。
<?php

    $MYarray=
    array(
    0 => array(
           "id"=> 1204,
           "custom_price"=> 33.1500
        ),
    1 =>  array(
           "id"=> 1199,
           "custom_price"=> 16.83
        ),
    2 => array(
           "id"=> 1176,
           "custom_price"=> 316.83
        ));


    $custom_price = array();
    foreach ($MYarray as $key => $row)
    {
        $custom_price[$key] = $row['custom_price'];
    }

    array_multisort($custom_price, SORT_DESC, $MYarray);


    var_dump($MYarray);
    ?>

0

Use Ksort

$age=array("1204"=>"33.1500","1199"=>"16.83","1176"=>"11.73");
ksort($age);

foreach($age as $x=>$x_value)
   {
    echo "Value=" . $x_value;
    echo "<br>";
   }

Output

Value=11.73
Value=16.83
Value=33.1500

Tip: Use the krsort() function to sort an associative array in descending order, according to the key.

Tip: Use the asort() function to sort an associative array in ascending order, according to the value.


0
// Descending order 
    function sortByDecOrder($a, $b) {
        return $b['custom_price'] - $a['custom_price'];
    }

    usort($arr, 'sortByOrder');
// Ascending order 
function sortByAscOrder($a, $b) {
        return $b['custom_price'] - $a['custom_price'];
    }

    usort($arr, 'sortByOrder');

0
你可以使用 usort 函数。
<?php 
$array = array( 
            "0" => array (
               "id" => 1204,
               "custom_price" => 33.1500    
            ),

            "1" => array (
               "id" => 1199,
               "custom_price" => 20.83  
            ),

            "2" => array (
               "id" => 1176,
               "custom_price" => 19.83  
            )
         );

usort($array, function($a, $b) {
    return $a['custom_price'] - $b['custom_price'];
});
echo "<pre>";
print_r($array);

检查所期望的输出


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接