5

I have this array in PHP:

In PHP APIs I have this array and want to sort ot by custom_price, but not getting how to acheive so ..

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

How i can sort from .. high to low & low to high .. by custom_price

Eddie
  • 26,593
  • 6
  • 36
  • 58
Narendra.vyas
  • 153
  • 1
  • 1
  • 6

6 Answers6

9

Using usort:

high to low

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

low to high

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

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

Karlo Kokkak
  • 3,674
  • 4
  • 18
  • 33
1

This solution might help you.

function sortByOrder($a, $b) {
    return $a['custom_price'] - $b['custom_price'];
}

usort($myArray, 'sortByOrder');

Or

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

here is reference link

Jalpesh Patel
  • 3,150
  • 10
  • 44
  • 68
1

use array_multisort() with SORT_DESC and SORT_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);
    ?>
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
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.

TarangP
  • 2,711
  • 5
  • 20
  • 41
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');
Jignesh Patel
  • 1,028
  • 6
  • 10
0

You can use usort function

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

check the desired output

Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42