8

I have an array like one mentioned below

Array
(
[6] => Array
    (
        [name] => Extras
        [total_products] => 0
        [total_sales] => 0
        [total_affiliation] => 0
    )

[5] => Array
    (
        [name] => Office Products
        [total_products] => 7
        [total_sales] => 17
        [total_affiliation] => 8
    )

[1] => Array
    (
        [name] => Hardware Parts
        [total_products] => 6
        [total_sales] => 0
        [total_affiliation] => 0
    )

)

Right now, order is: Extras, Office Products, Hardware Parts

I want to sort main array in such as way that it is order by total_sales of inner-array in desc order

so order will be: Office Products, Extras, Hardware Parts

Any help guys

I-M-JM
  • 15,732
  • 26
  • 77
  • 103
  • In general, anytime you need to sort an array in a special way and the regular sorting functions won't do the job, you should take a look at the [u-sort family of functions](http://php.net/manual/en/array.sorting.php). –  Jun 30 '11 at 06:27

3 Answers3

17

PHP 5.3:

usort($array, function ($a, $b) { return $b['total_sales'] - $a['total_sales']; });

PHP 5.2-:

usort($array, create_function('$a,$b', 'return $b["total_sales"] - $a["total_sales"];'));
deceze
  • 510,633
  • 85
  • 743
  • 889
3

Use a custom function and usort:

<?php
function custom_sale_sort($a, $b)
{
    if ($a['total_sales'] < $b['total_sales'])
        return 1;
    elseif ($a['total_sales'] == $b['total_sales'])
        return 0;
    else
        return -1;
}

usort($array, 'custom_sale_sort');

If you need your array sorted in the other direction, then switch the (1,-1) values around in the custom function.

  • 1
    can be simplified from if-else by simply returning difference of $a['total_sales'] and $b['total_sales'] as usort checks -ve,+ve not magnitude to assign order. – DhruvPathak Jun 30 '11 at 06:33
2

Here is the class you can use to do multidimension sort

Note: You must have PHP5

class MultiDimensionSort
{
    const ASCENDING = 0,DESCENDING = 1;

    public  $sortColumn,$sortType;

    public function __construct($column = 'price', $type = self::ASCENDING)
    {
        $this->column = $column;
        $this->type = $type;
    }

    public function cmp($a, $b)
    {
        switch($this->type)
        {
            case self::ASCENDING:
                return ($a[$this->column] == $b[$this->column]) ? 0 : (($a[$this->column] < $b[$this->column]) ? -1 : 1);
            case self::DESCENDING:
                return ($a[$this->column] == $b[$this->column]) ? 0 :(($a[$this->column] < $b[$this->column]) ? 1 : -1);
            default:
                assert(0); // unkown type
        }
    }
}

Like you have array named summary with contain above array. than you can do sort by following statements. // assuming your array variable is $summary

$s = new MultiDimensionSort('total_sales', MultiDimensionSort::DESCENDING); // sort by total_sales

usort($summary, array($s, 'cmp'));
print"<pre>";print_r($summary);

Cheers! May be this ll help you

user822391
  • 21
  • 2
  • 2
    Isn't that just *slightly* overkill? :) – deceze Jun 30 '11 at 06:32
  • A whole class wrapper where a one-liner would do...? If you're going OO, why not abstract it further to get something like `MultiDimensionSort::sort($array, 'total_sales', MultiDimensionSort::DESCENDING)`? – deceze Jun 30 '11 at 06:41